PHP日期返回错误的时间 [英] PHP date returning wrong time

查看:99
本文介绍了PHP日期返回错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下脚本在我调用 date_default_timezone_set(UTC)后返回错误的时间

<?PHP   
    $timestamp = time();
    echo "<p>Timestamp: $timestamp</p>";

    // This returns the correct time
    echo "<p>". date("Y-m-d H:i:s", $timestamp) ."</p>";


    echo "<p>Now I call 'date_default_timezone_set(\"UTC\")' and echo out the same timestamp.</p>";
    echo "Set timezone = " . date_default_timezone_set("UTC");

    // This returns a time 5 hours in the past
    echo "<p>". date("Y-m-d H:i:s", $timestamp) ."</p>";

?>

服务器上的时区是 BST 。那么应该发生的是第二次调用'date'应该在第一次调用后1个小时返回一个时间。实际上回到第一个5个小时后的时间。

The timezone on the server is BST. So what should happen is that the second call to 'date' should return a time 1 hour behind the first call. It's actually returning a time 5 hours behind the first one.

我应该注意,服务器最初设置为EDT时区(UTC -4)。已更改为BST(UTC +1),服务器已重新启动。

I should note that the server was originally set up with the EDT timezone (UTC -4). That was changed to BST (UTC +1) and the server was restarted.

我不知道这是一个PHP问题还是服务器的问题。

I can't figure out if this is a PHP problem or a problem with the server.

推荐答案

这几乎肯定不是php中的错误,但在本地时区配置中。

This is almost certainly not an error in php, but in your local timezone configuration.

最有可能的是,实际上并没有改变系统范围的时区,只是改变了交互式显示。检查 / etc / localtime 是否符合您的期望。在debian系统上,您可以运行tzselect(具有超级用户权限)来设置系统范围的时区。

Most likely, you didn't actually change the system-wide time zone, but only that of an interactive display. Check that /etc/localtime matches what you'd expect. On debian systems, you can run tzselect (with superuser privileges) to set the system-wide timezone.

设置时区后,可能需要重置时钟。在许多系统上,这应该会随时间自动发生,但您可以通过运行 ntpdate -u pool.ntp.org 手动执行。

After setting the timezone, you may have to reset your clock. On many systems, that should happen automatically over time, but you can do it manually by running ntpdate -u pool.ntp.org.

这篇关于PHP日期返回错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆