我有一个代码
echo date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td></tr>';
结果:
23-10-2013 16:28
有没有办法变成这样:
Posted 12 minutes ago
或者 / &
Just Posted
这样做最简单的方法是什么?
要获取当前时间和发布时间之间的秒差:
$number_of_seconds = strtotime("now") - strtotime($posts_row['post_date']);
然后,您可以应用您的逻辑来显示几秒前、刚刚等。
strtotime将帮助您转换日期并对其进行操作。
function elapsedTimeAgo ($newTime) {
$timeCalc = time() – strtotime($newTime);
$elapsedTimeText = "";
if ($timeCalc > (60*60*24)) {
$elapsedTimeText = round($timeCalc/60/60/24) . "days ago";
} else if ($timeCalc > (60*60)) {
$elapsedTimeText = round($timeCalc/60/60) . "hours ago";
} else if ($timeCalc > 60) {
$elapsedTimeText = round($timeCalc/60) . "minutes ago";
} else if ($timeCalc > 0) {
$elapsedTimeText .= "seconds ago";
} else {
$elapsedTimeText .= "Just Posted";
}
return $elapsedTimeText;
}
echo elapsedTimeAgo($posts_row['post_date']);