我有一个代码
echo date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td></tr>';
结果:
23-10-2013 16:28
有没有办法改变成:
Posted 12 minutes ago
或/&amp;
Just Posted
这是最简单的方法吗?
答案 0 :(得分:0)
获取当前时间与发布时间之间的秒数差异:
$number_of_seconds = strtotime("now") - strtotime($posts_row['post_date']);
然后,您可以应用逻辑来显示前几秒,等等。
答案 1 :(得分:0)
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']);