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


What is the proper way of converting a timestamp to a human readable date?

我有两个时间戳:$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

任何想法?由于

如果您使用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');
?>

由于你所在的时区,你会多出4个小时。unix时间戳是从UTC时间1970-01-01 00:00:00开始的秒数。如果您(或您的服务器)使用UTC+ 4tz,那么date()将隐式地执行时区转换到您的本地时间。

解决方案吗?使用gmdate()代替

您需要正确设置时区。http://www.php.net/manual/en/function.date-default-timezone-set.php