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

时间:2022-01-27 16:27:10

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

2008-12-13 10:42:00

2010-10-20 08:10:00

2010-10-20 08:10:00

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

我想要得到(h:m:s)格式的总时差

3 个解决方案

#1


23  

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

如果您正在使用或能够使用PHP 5.3。或以后,您可以使用它的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。

#2


13  

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

你现在可以按自己的方式格式化

#3


4  

You can use the the strtotime function to 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

#1


23  

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

如果您正在使用或能够使用PHP 5.3。或以后,您可以使用它的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。

#2


13  

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

你现在可以按自己的方式格式化

#3


4  

You can use the the strtotime function to 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