可能重复:
如何使用PHP计算两个日期之间的差异?
在这里,我提到它的日期两次
2008-12-13 10:42:00
2010-10-20 08:10:00
我希望得到(h:m:s)格式的总时差
fut*_*eal 27
如果您正在使用或能够使用PHP 5.3.x或更高版本,则可以使用其DateTime对象功能:
$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');
Run Code Online (Sandbox Code Playgroud)
您可以通过各种方式使用该格式,并且在DateTime对象中有日期后,您可以利用许多不同的功能,例如通过普通运算符进行比较.有关更多信息,请参阅手册:http://us3.php.net/manual/en/datetime.diff.php
小智 16
什么即时使用:
$seconds = strtotime("2010-10-20 08:10:00") - strtotime("2008-12-13 10:42:00");
$days = floor($seconds / 86400);
$hours = floor(($seconds - ($days * 86400)) / 3600);
$minutes = floor(($seconds - ($days * 86400) - ($hours * 3600))/60);
$seconds = floor(($seconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));
Run Code Online (Sandbox Code Playgroud)
你可以按自己的方式格式化
您可以使用strtotime 函数将时间转换为整数并减去它们。
$time1 = strtotime("2008-12-13 10:42:00");
$time2 = strtotime("2010-10-20 08:10:00");
$diff = $time2-$time1;
// the difference in int. then you can divide by 60,60,24 and
// so on to get the h:m:s out of it
Run Code Online (Sandbox Code Playgroud)