php計算給定時間之前的函數(shù)用法實例
更新時間:2015年04月03日 11:56:28 作者:不吃皮蛋
這篇文章主要介紹了php計算給定時間之前的函數(shù)用法,實例分析了php計算時間的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php計算給定時間之前的函數(shù)用法。分享給大家供大家參考。具體如下:
這里給定一個時間,計算這個時間在多久前,比如:2天前,1年前
<?php function prettyDate($date){ $time = strtotime($date); $now = time(); $ago = $now - $time; if($ago < 60){ $when = round($ago); $s = ($when == 1)?"second":"seconds"; return "$when $s ago"; }elseif($ago < 3600){ $when = round($ago / 60); $m = ($when == 1)?"minute":"minutes"; return "$when $m ago"; }elseif($ago >= 3600 && $ago < 86400){ $when = round($ago / 60 / 60); $h = ($when == 1)?"hour":"hours"; return "$when $h ago"; }elseif($ago >= 86400 && $ago < 2629743.83){ $when = round($ago / 60 / 60 / 24); $d = ($when == 1)?"day":"days"; return "$when $d ago"; }elseif($ago >= 2629743.83 && $ago < 31556926){ $when = round($ago / 60 / 60 / 24 / 30.4375); $m = ($when == 1)?"month":"months"; return "$when $m ago"; }else{ $when = round($ago / 60 / 60 / 24 / 365); $y = ($when == 1)?"year":"years"; return "$when $y ago"; } } echo prettyDate("2012-07-22 12:23:45")."<br />"; echo prettyDate("2010-11-12 22:25:45")."<br />"; echo prettyDate("2012-01-01 01:00:00")."<br />"; echo prettyDate("2001-05-30 00:00:00")."<br />";
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
如何獲知PHP程序占用多少內(nèi)存(memory_get_usage)
想要知道編寫的 PHP 腳本需要占用多少內(nèi)存么?很簡單,直接使用 PHP 查看當前分配給 PHP 腳本的內(nèi)存的函數(shù) memory_get_usage() 就可以了2012-09-09PHP中空字符串介紹0、null、empty和false之間的關(guān)系
用PHP開發(fā)那么久,PHP中空字符串、0、null、empty和false之間的關(guān)系總是有些不確定的東西。遇到它們應(yīng)該用哪個方法函數(shù)去處理2012-09-09