What i am doing is:
echo date('H:i:s', abs(strtotime("10:50:00 - 08:50:00")));
And what i am getting is:
03:00:00
Timezone settings aint helping. Different servers / machines act the same. 10-8 must equal to 2 not 3. I know that easy solution would be -3600. But i want it to work without patching mistakes.
Using DateTime objects and DateInterval
$d1 = new DateTime("10:50:00");
$d2 = new DateTime("08:50:00");
$diff = $d1->diff($d2);
echo $diff->format('%H:%I:%S');
strtotime
converts time to a Unix timestamp, which is basically the time in seconds since 1970, which is an integer.
So you can convert the two times and do a simple subtraction, then convert back using the date.
echo date('H:i:s', strtotime("10:50:00") - strtotime("08:50:00"));
The problem is, that strtotime
does not calculate differences. It tries to parse absolute dates. So the result is false
. abs(false) === 0
. Then you're left with
date('H:i:s', 0);
If your timezone is set to GMT-0300, then date()
tries to represent 1970-01-01T03:00:00.