conerting date('d-m-Y H:i in php to ago


conerting date('d-m-Y H:i in php to ago

我有一个代码

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

最简单的方法是什么?

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']);

获取当前时间和发布时间之间的秒差:

$number_of_seconds = strtotime("now") - strtotime($posts_row['post_date']);

然后,您可以应用逻辑来显示多少秒前、刚才等。