php實現求相對時間函數
文章主要介紹了php實現求相對時間函數,可實現簡單求相對時間為幾分鐘前或幾小時前的功能,非常簡單實用,需要的朋友可以參考下。
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;
}
希望本文所述對大家的php程序設計有所幫助。
php時間函數用法分析例 1. 用 microtime() 對腳本的運行計時
?
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()取得一個日期的 Unix 時間戳
int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
參數可以從右向左省略,任何省略的參數會被設置成本地日期和時間的當前值
date()格式化一個本地時間/日期
string date ( string format [, int timestamp] )
提示: 自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了發起該請求時刻的'時間戳。
strtotime -- 將任何英文文本的日期時間描述解析為 Unix 時間戳
?
1
2
echo strtotime("+1 day"), "/n";
echo strtotime("+1 week"), "/n";
例2. 某個時間的後一天,後一月
?
1
2
3
strtotime("+1 day ".$day);
strtotime("2008-01-31 +1 month");
strtotime($day." +1 day");
-
PHP面試之php自帶的幾個防止sql注入的函數
畢業季又到了,同學們是不是正為此煩惱呢?以下是本站小編精心為大家整理的PHP面試之php自帶的幾個防止sql注入的函數,希望對大家有所幫助!更多內容請關注應屆畢業生網!SQL注入攻擊是黑客攻擊網站最常用的手段。如果你的站點沒有使用嚴格的用户輸入檢驗,那麼常容易...
-
php遞歸創建和刪除文件夾的代碼
php中遞歸創建和刪除文件夾的代碼,供大家學習參考。就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆畢業生考試網!方法一複製代碼代碼如下:<?php/***目錄生成類:UtilsMakeDir*@authoryepeng*@since2010.3.18*/classUtilsMakeDir{//基目錄建...
-
PHP入門學習方法
每個人的學習方式不同,寫這篇文章的目的是分享一下自己的學習過程,僅供參考,不要一味的用別人的學習方法,找對自己有用的學習方式,關於php語言怎麼學呢?下面是相關的知識,歡迎閲讀。其實學習一門新語言並不是太難,重要的是你有沒有準備好去學好它,時間的長短和個人的能...
-
PHP 關於訪問控制的和運算符優先級介紹
導語:PHP的知識點相對來説比較多,而且不容易記憶,下面的是本站小編為大家蒐集的.是PHP關於訪問控制的和運算符優先級介紹,希望對你能有所幫助。代碼如下:classFoo{private$name='hdj';publicfunctiongetName(){return$this->name;}}classBarextendsFoo{pub...