php實現(xiàn)求相對時間函數(shù)
更新時間:2015年06月15日 15:13:46 作者:小卒過河
這篇文章主要介紹了php實現(xiàn)求相對時間函數(shù),可實現(xiàn)簡單求相對時間為幾分鐘前或幾小時前的功能,非常簡單實用,需要的朋友可以參考下
本文實例講述了php實現(xiàn)求相對時間函數(shù)。分享給大家供大家參考。具體實現(xiàn)方法如下:
<?php
function relativeTime($time = false, $limit = 86400, $format = 'g:i A M jS') {
if (empty($time) || (!is_string($time) & amp; & amp;
!is_numeric($time))) $time = time();
elseif (is_string($time)) $time = strtotime($time);
$now = time();
$relative = '';
if ($time === $now) $relative = 'now';
elseif ($time > $now) $relative = 'in the future';
else {
$diff = $now - $time;
if ($diff >= $limit) $relative = date($format, $time);
elseif ($diff < 60) {
$relative = 'less than one minute ago';
} elseif (($minutes = ceil($diff / 60)) < 60) {
$relative = $minutes . ' minute' . (((int)$minutes === 1) ? '' : 's') . ' ago';
} else {
$hours = ceil($diff / 3600);
$relative = 'about ' . $hours . ' hour' . (((int)$hours === 1) ? '' : 's') . ' ago';
}
}
return $relative;
}
希望本文所述對大家的php程序設(shè)計有所幫助。
您可能感興趣的文章:
- PHP與Java對比學(xué)習(xí)日期時間函數(shù)
- php時間函數(shù)用法分析
- php時間戳格式化顯示友好的時間函數(shù)分享
- php實現(xiàn)的DateDiff和DateAdd時間函數(shù)代碼分享
- PHP函數(shù)之日期時間函數(shù)date()使用詳解
- php date()日期時間函數(shù)詳解
- php checkdate、getdate等日期時間函數(shù)操作詳解
- PHP 日期時間函數(shù)的高級應(yīng)用技巧
- PHP日期時間函數(shù)的高級應(yīng)用技巧
- 使用PHP的日期與時間函數(shù)技巧
- php Mysql日期和時間函數(shù)集合
- PHP 常用時間函數(shù)資料整理
相關(guān)文章
php+js實現(xiàn)百度地圖多點標(biāo)注的方法
這篇文章主要介紹了php+js實現(xiàn)百度地圖多點標(biāo)注的方法,涉及php結(jié)合js針對百度地圖接口調(diào)用與json操作相關(guān)技巧,需要的朋友可以參考下2016-11-11
php.ini中date.timezone設(shè)置分析
date.timezone設(shè)置php5默認(rèn)date.timezone為utc,改為date.timezone = PRC即可解決時間相差八小時的問題,但我在php的官方文檔中看了半天也沒找到這個參數(shù)啊2011-07-07
如何在smarty中增加類似foreach的功能自動加載數(shù)據(jù)
本篇文章是對在smarty中增加類似foreach的功能自動加載數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
淺析虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決辦法
以下是對虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08

