如何在 PHP 中找到两个日期之间的时差? [英] How do I find the hour difference between two dates in PHP?
本文介绍了如何在 PHP 中找到两个日期之间的时差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个日期,格式为Y-m-d H:i:s".我需要比较这两个日期并找出时差.
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);
除以 3600,因为一小时有 3600 秒,使用 round()
避免小数点过多.
Dividing by 3600 because there are 3600 seconds in one hour and using round()
to avoid having a lot of decimal places.
这篇关于如何在 PHP 中找到两个日期之间的时差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文