tags:

views:

47

answers:

3

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
thanks, that is what I was trying, but I was trying to divide by 24.
mike
A: 
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 /  60;
Philippe Gerber
A: 

You can use strtotime() to parse your strings and do the difference between the two of them.


Resources :

Colin Hebert