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.
Asked
Active
Viewed 8.9k times
42
-
3Have you tried anything? For example you could convert them both into unix timestamps (seconds) using `strtotime`, then subtract that, then divide by 3600 to get hours – Fanis Hatzidakis Sep 21 '10 at 18:56
-
Yes, I was dividing by 24 instead of 3600. doh! – mike Sep 21 '10 at 19:05
10 Answers
83
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
- 23,354
- 24
- 59
- 84
-
-
5Using `strtotime()` is **not the best way** managing dates. Consider using `\DateTime::createFromFormat($date, 'Y-m-d H:i:s')->diff($dto2)` or similar functionality to work with dates (_+ we know the format we expect to parse_). It's almost 2015 outside, lets do thing the elegant way! – Paul T. Rawkeen Nov 28 '14 at 13:11
-
1except that createFromFormat takes $format as the first parameter ;) `\DateTime::createFromFormat('Y-m-d H:i:s', $date)->diff( $dto2 )` – Asmodiel Mar 08 '17 at 17:32
34
You can use DateTime class also -
$d1= new DateTime("06-08-2015 01:33:26pm"); // first date
$d2= new DateTime("06-07-2015 10:33:26am"); // second date
$interval= $d1->diff($d2); // get difference between two dates
echo ($interval->days * 24) + $interval->h; // convert days to hours and add hours from difference

Sougata Bose
- 31,517
- 8
- 49
- 87
7
As an addition to accepted answer I would like to remind that \DateTime::diff
is available!
$f = 'Y-m-d H:i:s';
$d1 = \DateTime::createFromFormat($date1, $f);
$d2 = \DateTime::createFromFormat($date2, $f);
/**
* @var \DateInterval $diff
*/
$diff = $d2->diff($d1);
$hours = $diff->h + ($diff->days * 24); // + ($diff->m > 30 ? 1 : 0) to be more precise
\DateInterval
documentation.

Paul T. Rawkeen
- 3,994
- 3
- 35
- 51
7
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;

Philippe Gerber
- 17,457
- 6
- 45
- 40
3
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

Vishal
- 31
- 3
1
$date1 = date_create('2016-12-12 09:00:00');
$date2 = date_create('2016-12-12 11:00:00');
$diff = date_diff($date1,$date2);
$hour = $diff->h;
-
1This is not accurate as if there is more than 24h of difference, this could easily return 0 (ie. $diff->d = 1 and $diff->h = 0 ) – Cyrille Armanger Oct 04 '21 at 07:43
0
You can try this:
$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);

Nisse Engström
- 4,738
- 23
- 27
- 42

Govinda Yadav
- 539
- 4
- 14
0
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";

eedue
- 1
-1
You can use strtotime()
to parse your strings and do the difference between the two of them.
Resources :

Colin Hebert
- 91,525
- 15
- 160
- 151