我使用這個...它處理過去和未來的時間。
/**
* Get a string representing the duration difference in two timestamps
*
* @param int $time The timestamp to compare
* @param int $timeBase The base timestamp to compare against, defaults to time()
* @return string Returns the duration summary
*/
public function getTimeSummary ($time, $timeBase = false) {
if (!$timeBase) {
$timeBase = time();
}
if ($time <= time()) {
$dif = $timeBase - $time;
if ($dif < 60) {
if ($dif < 2) {
return "1 second ago";
}
return $dif." seconds ago";
}
if ($dif < 3600) {
if (floor($dif/60) < 2) {
return "A minute ago";
}
return floor($dif/60)." minutes ago";
}
if (date("d n Y", $timeBase) == date("d n Y", $time)) {
return "Today, ".date("g:i A", $time);
}
if (date("n Y", $timeBase) == date("n Y", $time) && date("d", $timeBase) - date("d", $time) == 1) {
return "Yesterday, ".date("g:i A", $time);
}
if (date("Y", $time) == date("Y", time())) {
return date("F, jS g:i A", $time);
}
} else {
$dif = $time - $timeBase;
if ($dif < 60) {
if ($dif < 2) {
return "1 second";
}
return $dif." seconds";
}
if ($dif < 3600) {
if (floor($dif/60) < 2) {
return "Less than a minute";
}
return floor($dif/60)." minutes";
}
if (date("d n Y", ($timeBase + 86400)) == date("d n Y", ($time))) {
return "Tomorrow, at ".date("g:i A", $time);
}
}
return date("F, jS g:i A Y", $time);
}
像[日期/時間](http://us.php.net/DateTime)這樣的軟件包一般可以幫助你。哦,'$ deff' *是*秒。 – 2010-08-12 18:40:13
我在這裏錯過了什麼嗎?使用'string date(string $ format [,int $ timestamp])''有什麼問題? – Incognito 2010-08-12 18:41:53