php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù)
更新時(shí)間:2015年06月15日 15:13:46 作者:小卒過河
這篇文章主要介紹了php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù),可實(shí)現(xiàn)簡(jiǎn)單求相對(duì)時(shí)間為幾分鐘前或幾小時(shí)前的功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
本文實(shí)例講述了php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù)。分享給大家供大家參考。具體實(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;
}
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- PHP與Java對(duì)比學(xué)習(xí)日期時(shí)間函數(shù)
- php時(shí)間函數(shù)用法分析
- php時(shí)間戳格式化顯示友好的時(shí)間函數(shù)分享
- php實(shí)現(xiàn)的DateDiff和DateAdd時(shí)間函數(shù)代碼分享
- PHP函數(shù)之日期時(shí)間函數(shù)date()使用詳解
- php date()日期時(shí)間函數(shù)詳解
- php checkdate、getdate等日期時(shí)間函數(shù)操作詳解
- PHP 日期時(shí)間函數(shù)的高級(jí)應(yīng)用技巧
- PHP日期時(shí)間函數(shù)的高級(jí)應(yīng)用技巧
- 使用PHP的日期與時(shí)間函數(shù)技巧
- php Mysql日期和時(shí)間函數(shù)集合
- PHP 常用時(shí)間函數(shù)資料整理
相關(guān)文章
php+js實(shí)現(xiàn)百度地圖多點(diǎn)標(biāo)注的方法
這篇文章主要介紹了php+js實(shí)現(xiàn)百度地圖多點(diǎn)標(biāo)注的方法,涉及php結(jié)合js針對(duì)百度地圖接口調(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即可解決時(shí)間相差八小時(shí)的問題,但我在php的官方文檔中看了半天也沒找到這個(gè)參數(shù)啊2011-07-07
php對(duì)xml文件的增刪改查操作實(shí)現(xiàn)方法分析
這篇文章主要介紹了php對(duì)xml文件的增刪改查操作實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了php對(duì)xml文件的載入及xml節(jié)點(diǎn)的讀取、修改、查詢等相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
如何在smarty中增加類似foreach的功能自動(dòng)加載數(shù)據(jù)
本篇文章是對(duì)在smarty中增加類似foreach的功能自動(dòng)加載數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
淺析虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決辦法
以下是對(duì)虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08

