php 使用php获取两个日期之间的总时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10696654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:51:20  来源:igfitidea点击:

Get total time difference between two dates using php

php

提问by meginjoy

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

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

Here i mention two times with its date

在这里我提到了两次它的日期

2008-12-13 10:42:00

2010-10-20 08:10:00

2008-12-13 10:42:00

2010-10-20 08:10:00

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

我想以 (h:m:s) 格式获得总时差

回答by futureal

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

如果您正在使用或能够使用 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');

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

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

回答by futureal

what im using:

我在用什么:

$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)));

you can format now in your way

你现在可以按照你的方式格式化

回答by Ibu

You can use the the strtotime functionto turn the time to integers and subtract them.

您可以使用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