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}"; }

我传递功能的日期为“2011-01-16 12:30”,但我得到恢复,这意味着劣枣$unix_date为空,但是如果我在死的功能我得到的$date传递给然而功能它得到与返回(在它之前,下面是我如何调用该方法。

echo nicetime(date('Y:m:d G:i', $a['created_at']))

--------------解决方案-------------

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是你认为它是。

分类:php 时间:2015-03-15 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.623 (s). 10 q(s)