我正在寻找一个解决方案:我在幻灯片中有一个 foreach 循环(帖子),我需要检索自发布日期以来经过的时间。我正在关注这个线程PHP How to find the time elapsed since a date time? 这就是我到目前为止所做的,但它不起作用,它破坏了幻灯片,或者我收到了一个关于 $time variabe 的错误,无法重新清除。有人可以帮忙吗?谢谢。
<?php if(count($comments) > 0): ?>
<?php foreach($comments as $comment): ?>
<?php
$authorPicture = $comment['authorPicture'];
$author = $comment['author'];
$image = $comment['image'];
$message = $comment['message'];
$likes = $comment['likes'];
$type = $comment['type'];
$data = $comment['cacheDate'];
$time = substr($data, 0, -3); //need to do this to retrieve a correct Unix timestamp
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
?>
<div class="data"><?php echo 'event happened '.humanTiming($time).' ago'; ?></div>
<?php endforeach; ?>
<?php else: ?>
<h2>There are no comments yet</h2>
<?php endif; ?>