私は解決策を探しています: スライドショー内に (投稿の) foreach ループがあり、公開日からの経過時間を取得する必要があります。私はこのスレッドをフォローしていますPHP 日時から経過した時間を見つける方法は? これは私がこれまで行ってきたことですが、機能しません。スライドショーが壊れるか、再宣言できない $time 変数に関するエラーが発生しました。誰か助けてくれませんか?ありがとう。
<?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; ?>