How do I find the hour difference between two date

2020-01-24 07:31发布

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.

9条回答
萌系小妹纸
2楼-- · 2020-01-24 08:03
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
查看更多
放我归山
3楼-- · 2020-01-24 08:12
$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;
查看更多
我命由我不由天
4楼-- · 2020-01-24 08:13

The problem is that using these values the result is 167 and it should be 168:

$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 /  60;
查看更多
时光不老,我们不散
5楼-- · 2020-01-24 08:16

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.

查看更多
forever°为你锁心
6楼-- · 2020-01-24 08:17

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


Resources :

查看更多
Explosion°爆炸
7楼-- · 2020-01-24 08:17

You can try this:

$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);
查看更多
登录 后发表回答