Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

php

datetime

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.

like image 601
mike Avatar asked Sep 21 '10 18:09

mike


People also ask

How do I find the hour difference between two dates?

Dates and times in Excel are stored as a date number, which is the number of days since 1 January 1900, with the value after the decimal point representing the time of day. To calculate the number of hours between two dates we can simply subtract the two values and multiply by 24.

How can I get the difference between two dates in php?

PHP date_diff() Function $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2);

How can I calculate total hours in php?

There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format.


1 Answers

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.

like image 110
Aillyn Avatar answered Sep 19 '22 20:09

Aillyn