【问题标题】:conerting date('d-m-Y H:i in php to ago [duplicate]将 php 中的日期('d-m-Y H:i 转换为之前 [重复]
【发布时间】:2013-11-02 14:10:41
【问题描述】:

我有密码

echo date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td></tr>';

结果:

23-10-2013 16:28

有没有办法改成like:

Posted 12 minutes ago

或/&

Just Posted

最简单的方法是什么?

【问题讨论】:

  • 用“发布日期”减去当前时间怎么样?试试看!
  • 我是 php 菜鸟...请帮忙。我可以减去数字,但是当涉及到时间时......我不会得到它

标签: php time converter


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    然后,您可以应用您的逻辑来显示几秒前、刚刚等。

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2021-02-26
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多