将时间戳转换为人类可读日期的正确方法是什么?

时间:2011-08-17 20:48:54

标签: php date time

我有2张时间戳:$start_time = 1312346227;$end_time = 1312346466; 我试图减去它们以查看$end_time = $end_time - $start_time;

之间的时间

我得到239

将此转换为人类可读日期的正确方法是什么?

如果我尝试echo date("h:i:s A",$end_time);我得到04:03:59,那应该是00:03:59

任何想法? 感谢

3 个答案:

答案 0 :(得分:4)

如果你有PHP 5.3,请使用DateInterval类。

DateInterval::format()上的手册页中隐藏的示例:

<?php

$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);

// %a will output the total number of days.
echo $interval->format('%a total days')."\n";

// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');

?>

答案 1 :(得分:3)

由于您的时区,您需要四个小时的时间。请记住,unix时间戳是自1970-01-01 00:00:00 UTC以来的秒数。如果您(或您的服务器)处于UTC + 4 TZ,那么date()将隐式地将时区转换为您当地的时间。

解决方案?请改用gmdate()

答案 2 :(得分:1)

相关问题