How do I find the time difference between two timestamps?

130 views Asked by At

Let's say I have two mongo dates.

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

So now I have two timestamps to compare, but I need to figure out, if dateB is any later than a day more than dateA.

so if the difference between the two dates is 1 day, I need to perform another tasks, but I have no idea how to get the difference?

How do I tackle this?

2

There are 2 answers

0
Phil On BEST ANSWER

Use a 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
xdazz On

They are unix timestamp, the unit is second, so just:

if ($b - $a > 86400) {
    // do something
}