我给这个函数传递了一个mysql日期时间,但它没有返回任何信息。mysql日期时间与strtotime()不兼容吗?我假设foreach循环正在结束,并且没有调用返回变量,但为什么呢?
function timeAgo($time) {
$time = time() - strtotime($time); // to get the time since that moment
$tokens = array (
1 => 'second',
60 => 'minute',
3600 => 'hour',
86400 => 'day',
604800 => 'week',
2592000 => 'month',
31536000 => 'year'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
}
}
Update这是从数据库中获取它的代码,日期时间字符串中没有引号。
while ($row = mysqli_fetch_assoc($result)) {
$commentTime = $row["time"];
$commentComment = $row["comment"];
$commentCommenter = $row["commenter"];
}
这是回显它的代码:echo '<h3 class="party-time">'.timeAgo($commentTime).'</h3>';
问题是时区问题。我在使用的SQL服务器中手动设置日期,该服务器将日期时间设置为本地时间,但服务器的时区为-3小时,导致出现问题。
我们无法准确判断$time
包含什么,但当您将ISO-8601日期输入到strtotime
时,它可以正常工作:
php > echo strtotime("2013-08-01 12:00:00");
1375351200