2个时间戳之间的PHP小时,分钟和秒

时间:2013-02-04 17:08:49

标签: php timestamp

PHP 4

$a = 1359994013   (Mon, 04 Feb 2013 16:06:53)
$b = 1359997483   (Mon, 04 Feb 2013 17:04:43)

如何计算这两个时间戳之间的差异,并将结果显示为:

w days, x hours, y mins, z seconds.

例如: 0天,0小时,58分钟,10秒

谢谢:)

2 个答案:

答案 0 :(得分:6)

如果您正在计算并坚持使用PHP 4:

$a = 1359994013;
$b = 1359997483;

$difference = $b-$a;

$second = 1;
$minute = 60*$second;
$hour   = 60*$minute;
$day    = 24*$hour;

$ans["day"]    = floor($difference/$day);
$ans["hour"]   = floor(($difference%$day)/$hour);
$ans["minute"] = floor((($difference%$day)%$hour)/$minute);
$ans["second"] = floor(((($difference%$day)%$hour)%$minute)/$second);
echo $ans["day"] . " days, " . $ans["hour"] . " hours, "  . $ans["minute"] . " minutes, " . $ans["second"] . " seconds";

这会为您提供0 days, 0 hours, 57 minutes, 50 seconds

答案 1 :(得分:0)

您可以尝试使用日期功能:

http://php.net/manual/it/function.date.php