- 相關(guān)推薦
php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù)
文章主要介紹了php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù),可實(shí)現(xiàn)簡(jiǎn)單求相對(duì)時(shí)間為幾分鐘前或幾小時(shí)前的功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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時(shí)間函數(shù)用法分析
例 1. 用 microtime() 對(duì)腳本的運(yùn)行計(jì)時(shí)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
// Sleep for a while
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds/n";
?>
mktime()取得一個(gè)日期的' Unix 時(shí)間戳
int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
參數(shù)可以從右向左省略,任何省略的參數(shù)會(huì)被設(shè)置成本地日期和時(shí)間的當(dāng)前值
date()格式化一個(gè)本地時(shí)間/日期
string date ( string format [, int timestamp] )
提示: 自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了發(fā)起該請(qǐng)求時(shí)刻的時(shí)間戳。
strtotime -- 將任何英文文本的日期時(shí)間描述解析為 Unix 時(shí)間戳
?
1
2
echo strtotime("+1 day"), "/n";
echo strtotime("+1 week"), "/n";
例2. 某個(gè)時(shí)間的后一天,后一月
?
1
2
3
strtotime("+1 day ".$day);
strtotime("2008-01-31 +1 month");
strtotime($day." +1 day");
【php實(shí)現(xiàn)求相對(duì)時(shí)間函數(shù)】相關(guān)文章:
php取得當(dāng)前時(shí)間函數(shù)09-12
php自定義函數(shù)實(shí)現(xiàn)漢字分割替換06-01
php使用ftp函數(shù)實(shí)現(xiàn)簡(jiǎn)單上傳功能10-31
php實(shí)現(xiàn)utf-8轉(zhuǎn)unicode函數(shù)分享09-17
如何使用php自定義函數(shù)實(shí)現(xiàn)漢字分割替換08-18
PHP內(nèi)部函數(shù)的定義07-04
PHP類與構(gòu)造函數(shù)07-01