我正在向这个函数传递一个 mysql 日期时间,它什么也没返回。mysql datetime 与 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';
}
}
更新这是从数据库中获取它的代码,日期时间字符串中没有引号。
while ($row = mysqli_fetch_assoc($result)) {
$commentTime = $row["time"];
$commentComment = $row["comment"];
$commentCommenter = $row["commenter"];
}
这是回显它的代码:echo '<h3 class="party-time">'.timeAgo($commentTime).'</h3>';