I need to compare the time difference between "todays date and time" and two dates in the future. These dates are including time informations, too.
At the moment I'm having the problem that the "if condition" doesn't output the expected result.
That's the code and the live example:
$date_a = new DateTime('now');
$date_b2 = new DateTime('wednesday + 7 day 08:00');
$date_c2 = new DateTime('saturday + 7 day 20:00');
$intervaln = date_diff($date_a,$date_b2);
$interval2n = date_diff($date_a,$date_c2);
$diff1n = $intervaln->format('%d d. %h h. %i min.');
$diff2n = $interval2n->format('%d d. %h h. %i min.');
if ($diff1n > $diff2n) {
echo $diff1n;
}
else if ($diff1n < $diff2n) {
echo $diff2n;
}
As you see it outputs the second condition but it should be the first one since $diff1n
has the bigger time difference than $diff2n
.
What's the right way to compare these dates and times to each other?