比较PHP中的Unix时间戳 [英] Comparing unix timestamps in PHP

查看:70
本文介绍了比较PHP中的Unix时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我有:

$diff = abs(strtotime(date('m/d/Y h:i:s')) - strtotime($latest));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
echo floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

如何获得以秒为单位的差异?我尝试了以下方法:

How do I get the difference in seconds? I've tried the following:

$diff = abs(strtotime(date('m/d/Y h:i:s')) - strtotime($latest));

推荐答案

使用 DateTime 代替,这将使您的代码更简洁.

Use DateTime instead, it will make your code much cleaner.

$latest = new DateTime($latest);
$now = new DateTime();

$diff = $latest->diff($now);
echo $diff->format('%y years %m months %d days');

这篇关于比较PHP中的Unix时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆