Roggie Roggie - 1 month ago 6
PHP Question

difference between two pair of date in php

I was trying to get the difference between two date. Unfortunately I only get whole number and not the formatted as required. I have tried several ways but no luck. I need to deduct $HoldInterval from $TactInterval but the system get an error.
Below is my code which does not work as required.
I get request of 2 instead of 02h30m. Kindly advise what is the issue. Thanks.

$hd1 = new Datetime('2016-10-18 08:30:00');
$hd2 = new Datetime('2016-10-18 12:00:00');
$HoldInterval = $hd2->diff($hd1);

$d1 = new Datetime('2016-10-18 09:00:00');
$d2 = new Datetime('2016-10-18 10:00:00');
$TactInterval = $d2->diff($d1);
//$interval = $TactInterval - $HoldInterval ); // This is not working....

$dt1 = $TactInterval->format('%Hh%Im');
$dt2 = $HoldInterval->format('%Hh%Im');

echo $dt1."<br>";
echo $dt2."<br>";

$dt3 = $dt2 - $dt1; // This is working but only a whole number shows. it show be 02h30m.

//$dt4 = $dt3->format('%Hh%Im'); // does not work when formated.

echo $dt3."<br>";

Answer
$s = new DateTime();
$e = new DateTime();
$s->add($HoldInterval);
$e->add($TactInterval);
$diff = $e->diff($s);
echo $diff->format('%Hh%Im');

DEMO