PHP Hours, Mins and Seconds between 2 Timestamps

2019-02-20 16:38发布

问题:

PHP 4

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

How do I calculate the difference between these two timestamps and shows the results as:

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

Eg : 0 days, 0 hour, 58 mins, 10 seconds

Thanks :)

回答1:

If you are into the calculation and insist on using 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";

This gives you 0 days, 0 hours, 57 minutes, 50 seconds.



回答2:

You can try using the date function:

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



标签: php timestamp