且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-02 10:26:04

您可以将它们转换为时间戳,

  $ hourdiff = round((strtotime($ time1) -  strtotime($ time2))/ 3600,1); 

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


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.

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.