如何在PHP中找到两个日期之间的时差? [英] How do I find the hour difference between two dates in PHP?

查看:109
本文介绍了如何在PHP中找到两个日期之间的时差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日期,如Y-m-d H:i:s。我需要比较这两个日期,并找出时差。

解决方案

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

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

这篇关于如何在PHP中找到两个日期之间的时差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆