我有这个功能来计算自某个日期以来经过的相对时间
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}";
}
我将该函数的日期传递为“2011-01-16 12:30”,但是我收到了错误的日期,这意味着$unix_date
为空,但是如果我死在函数中我得到{ {1}}传递给函数然后它返回一个(在它之前,下面是我调用方法的方式。
$date
答案 0 :(得分:0)
echo nicetime("2011-01-16 12:30");
对我来说很好。我建议你确保
$a['created_at']
告诉你它是什么东西。也许试试
$dtDate = $a['created_at'];
$strDate = date('Y:m:d G:i', $dtDate );
echo $strDate;
echo nicetime($strDate);
确保$ strDate是您认为的。