如何使用PHP获得英国当地时间的日期?


How do I get a date in UK local time using PHP?

我使用unix时间戳来显示在我的项目中发布消息的时间,但是当我显示帖子的确切时间时,我意识到它落后了大约12小时。

我在英国,我的服务器在美国(可能是问题)。

是否有一种简单的方法将unix时间戳转换为可读的英国时间?

$timestamp = time();
print date("F jS, Y", strtotime($timestamp));

任何帮助将是伟大的,谢谢!

在你的脚本开头写上:

date_default_timezone_set('Europe/London');

或者如果你的PHP是>= 5.2.0:

date_timezone_set('Europe/London');

在显示日期(而不是当记录时)时,只需在脚本开头调用date_timezone_set,并为英国指定适当的参数;我不确定,但它可能会导致"错误"的时间戳被记录)。

Edit:您想要的时区是'Europe/London'

try date-default-timezone-set.

date_default_timezone_set('Europe/London');

Use date_default_timezone_set('Europe/London');将时区设置为伦敦时间。

最简单的方法是构造GMT偏移量。例句:

echo(date('Y-m-d h:i'), $myvalue - 60 * 60 * $nhours));
where $nhours -时差小时。

这个对我有用

date_default_timezone_set('Europe/London');