Search code examples
phpdatetimedate

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


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. How can I do that?


Solution

  • 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.