View Full Version : Date difference

10-26-2006, 10:06 AM
I am running an application on PHP with MySQL. i want to check the difference of two dates. I want to get the difference in hrs:min:sec. My field type is DateTime. I tried with mktime etc but still i could n't get the right value. please help.
Suppose date1 = 2006-01-01 10:05:00
Date2= 2006-01-01 17:55:00
i want the differnce between this?

10-26-2006, 02:00 PM
$diff = date("H:i:s", (strtotime($date2)-strtotime($date1));

10-27-2006, 11:10 AM
Thank u for ur reply. but still it is not giving the correct value. i written as below
$diff = date("H:i:s", (strtotime('2006-01-01 17:00:00')-strtotime('2006-01-01 10:00:00'));
Is it correct? is there any other method? i am fetching this date value from table.

10-27-2006, 06:35 PM
I checked this out, and the difference in seconds is being done correctly, but the date(...) function cannot be used to get an HH:MM:SS that this corresponds to.

The difference will be a number of seconds. When these are treated as a unix timestamp, they look like dates near January 1 1970 00:00:00 GMT. When the date(...) function is used, it gives the date/time that these small value timestamps correspond to relative to your current time zone. There will be an error in the number of hours that will be the difference between your time zone and GMT.

If you use the gmdate(...) function instead of date(...) it should work (it did for me.)