如何找到两个时间戳之间的时差?

时间:2015-06-11 01:16:46

标签: php timestamp difference

我们说我有两个mongo约会。

$a = $mongoDateA->sec;
$b = $mongoDateB->sec;

所以现在我有两个时间戳进行比较,但我需要弄清楚,如果dateB比dateA更晚一天。

所以,如果两个日期之间的差异是1天,我需要执行另一项任务,但我不知道如何找到差异?

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

使用DateTimeInterval

$dtA = new DateTime();
$dtA->setTimestamp($a);

$dtB = new DateTime();
$dtB->setTimestamp($b);

$diff = $dtA->diff($dtB);
if ($diff->days >= 1) {
    // perform other tasks
}

答案 1 :(得分:0)

它们是unix时间戳,单位是秒,所以只是:

if ($b - $a > 86400) {
    // do something
}
相关问题