如何在PHP中找到两个日期之间的小时差异?

时间:2021-06-18 21:28:17

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.

我有两个约会,形成像“Y-m-d H:i:s”。我需要比较这两个日期并找出小时差异。

9 个解决方案

#1


52  

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.

除以3600,因为在一小时内有3600秒并使用round()来避免有很多小数位。

#2


9  

You can use DateTime interface also -

你也可以使用DateTime接口 -

$d1= new DateTime("06-08-2015 01:33:26pm"); 
$d2= new DateTime("06-07-2015 10:33:26am");
$interval= $d1->diff($d2);
echo ($interval->days * 24) + $interval->h;

#3


4  

As an addition to accepted answer I would like to remind that \DateTime::diff is available!

作为接受答案的补充,我想提醒一下\ DateTime :: diff是可用的!

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

\ DateInterval文档。

#4


4  

$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;

#5


0  

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

您可以使用strtotime()来解析字符串并区分它们。


Resources :

资源:

#6


0  

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

问题是使用这些值的结果是167,它应该是168:

$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 /  60;

#7


0  

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

这是因为节省了一天的时间。夏令时(美国)2014年3月9日星期日凌晨2:00开始。

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

从$ date1 =“2014-03-07 05:49:23”到$ date2 =“2014-03-14 05:49:23”期间,您将失去一小时;

#8


0  

You can try this:

你可以试试这个:

$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);

#9


0  

$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;

#1


52  

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.

除以3600,因为在一小时内有3600秒并使用round()来避免有很多小数位。

#2


9  

You can use DateTime interface also -

你也可以使用DateTime接口 -

$d1= new DateTime("06-08-2015 01:33:26pm"); 
$d2= new DateTime("06-07-2015 10:33:26am");
$interval= $d1->diff($d2);
echo ($interval->days * 24) + $interval->h;

#3


4  

As an addition to accepted answer I would like to remind that \DateTime::diff is available!

作为接受答案的补充,我想提醒一下\ DateTime :: diff是可用的!

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

\ DateInterval文档。

#4


4  

$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;

#5


0  

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

您可以使用strtotime()来解析字符串并区分它们。


Resources :

资源:

#6


0  

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

问题是使用这些值的结果是167,它应该是168:

$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 /  60;

#7


0  

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

这是因为节省了一天的时间。夏令时(美国)2014年3月9日星期日凌晨2:00开始。

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

从$ date1 =“2014-03-07 05:49:23”到$ date2 =“2014-03-14 05:49:23”期间,您将失去一小时;

#8


0  

You can try this:

你可以试试这个:

$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);

#9


0  

$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;