有没有人,如果php已经在功能上内置了这个?
由于
答案 0 :(得分:6)
刚刚找到了一个完全符合我需要的功能
答案 1 :(得分:1)
不是原生的,但我对a similar question的回答可能对你有用。
答案 2 :(得分:1)
没有内置功能....但是以下功能可以做到。
<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago
?>
答案 3 :(得分:-1)
$date = "2009-03-04 17:45";
$result = nicetime($date);
只会返回2 Days Ago
但如果你想在1秒前,10秒前获得结果,你还需要在$ date中添加秒数
$date = "2009-03-04 17:45:20";
$result = nicetime($date);
因此,当你添加一个新的数据库条目并立即刷新页面时,你将在1秒前得到结果