Php substract time question

Hello forums I am really stumped why the following time is giving me 12 hours when it only elapsed 1 min 32 seconds or so …

$t1 = strtotime('23:00:39');
$t2 = strtotime ('23:01:07');
$e = date("h:i:s", $t2 - $t1); 

echo  $e;

The above gives me 12:00:28 :sick: why?

$t1 = strtotime('23:00:39');
$t2 = strtotime ('23:01:07');

$elapsedSeconds = $t2 - $t1; 

I have not tested this. But theoretically it should work.

Or essentially something like the following, should do the trick too (again untested).

$t1 = "23:00:39";
$t2 = "23:01:07";
$diff = strtotime($t2) - strtotime($t1);
$seconds_passed= $diff/3600; 
echo $seconds_passed." seconds have passed between ".$t1." and ".$t2; 

I am working with time so that one won’t do …

Maybe you need to manually set the date_default_timezone ?

something like :


Tnx wackyjoe

When I echo this date_default_timezone_get() it gives me UTC as my time zone.

however the values actually comes form the database and I converted them to gtm+8 on my query

something like this:

SELECT TIME(CONVERT_TZ(s_out,‘+00:00’,‘+8:00’)) AS time_out

Any work around for this that doesn’t involve setting the date_default_timezone_set(‘UTC’) to other timezones than UTC ?


so weired it only happens when the time Im subtracting is having the same hours
like :

23:40:20 - 23:31:00
19:20:00 - 19:00:00


But it works fine with

14:00:41 - 12:23:00

eekk !!

You’re trying to make a timer, but you’re using a calendar to do it. Remember that the date function isn’t a stopwatch, and what you’re getting is a time of day. That’s not 12 hours elapsed, it’s 12 o clock!

The difference is 28 seconds, and the ‘12’ comes from using a 12-hour clock and trying to display midnight (‘00’).

Use ‘H’ for the hours to display them in a 24-hour format and it should work, but remember it’ll only be effective up to 24 hours.

Nope it’s not a timer … anyway I just have a work around…pretty dirty eek

if (date("h", $t1) == date("h", $t2)){

$e = date("i:s", $t2-$t1);


I know it’s pretty dirty it makes me wanna puke just looking at it but it works so… well …:rolleyes:


Or just use ‘H’ for the hours instead :stuck_out_tongue:

I can’t it will be used on a mysql time field column. actually it should be:


Isn’t that exactly what

$e = date("H:i:s", $t2 - $t1); 

would return?