I have two dates, formated like "Y-m-d H:i:s". I need to compare these two dates and figure out the hour difference.
+8
A:
You can convert them to timestamps and go from there:
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Dividing by 3600 because there are 3600 seconds in one hour and using round()
to avoid having a lot of decimal places.
Aillyn
2010-09-21 18:56:06
thanks, that is what I was trying, but I was trying to divide by 24.
mike
2010-09-21 19:05:42
A:
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
Philippe Gerber
2010-09-21 18:56:32
A:
You can use strtotime()
to parse your strings and do the difference between the two of them.
Resources :
Colin Hebert
2010-09-21 18:56:42