使用PHP获取两个日期之间的总时差 [英] Get total time difference between two dates using php

查看:74
本文介绍了使用PHP获取两个日期之间的总时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何使用PHP计算两个日期之间的差? /a>

Possible Duplicate:
How to calculate the difference between two dates using PHP?

在这里我两次提及其日期

Here i mention two times with its date

2008-12-13 10:42:00

2008-12-13 10:42:00

2010-10-20 08:10:00

2010-10-20 08:10:00

我想获取(h:m:s)格式的总时差

I want to get total time difference in (h:m:s) format

推荐答案

如果您正在使用或能够使用PHP 5.3.x或更高版本,则可以使用其DateTime对象功能:

If you are using or able to use PHP 5.3.x or later, you can use its DateTime object functionality:

$date_a = new DateTime('2010-10-20 08:10:00');
$date_b = new DateTime('2008-12-13 10:42:00');

$interval = date_diff($date_a,$date_b);

echo $interval->format('%h:%i:%s');

您可以通过多种方式使用该格式,一旦DateTime对象中有日期,就可以利用许多不同的功能,例如通过常规运算符进行比较.有关更多信息,请参见手册: http://us3.php.net/manual/zh/datetime.diff. php

You can play with the format in a variety of ways, and once you have dates in DateTime objects, you can take advantage of a lot of different functionality, for example comparison via normal operators. See the manual for more: http://us3.php.net/manual/en/datetime.diff.php

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

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