How do I find the hour difference between two dates in PHP?

Viewed 80807

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.

10 Answers

You can try this :

    $time1 = new DateTime('06:56:58');
    $time2 = new DateTime('15:35:00');
    $time_diff = $time1->diff($time2);
    echo $time_diff->h.' hours';
    echo $time_diff->i.' minutes';
    echo $time_diff->s.' seconds';

Output:

8 hours 38 minutes 2 seconds

This is because of day time saving. Daylight Saving Time (United States) 2014 began at 2:00 AM on Sunday, March 9.

You lose one hour during the period from $date1 = "2014-03-07 05:49:23" to $date2 = "2014-03-14 05:49:23";

Related