使用 UNIX_TIMESTAMP 和 time() 时出现一个小时错误

发布于 2024-10-09 15:34:18 字数 905 浏览 0 评论 0原文

我已经设置了 mysql 数据库: SET time_zone = 'America/Los_Angeles'; 在 php 中,

date_default_timezone_set('America/Los_Angeles');   

但是当我将

$time = strtotime ("+2 minutes");

and

$time2 = gmdate("Y-m-d H:i:s", $time);  

添加到数据库时,当我从 mysql 获取它时,我会出错一个小时,如下所示:

        $query ="select UNIX_TIMESTAMP("time"), time2 from tbl; 
        if ($result = $conn->query($query)) {
            while ($row = $result->fetch_assoc()) {
                $unix_time = $row["UNIX_TIMESTAMP("time")"];
                $unix_time2 = $row["time2"];
            }
            $result->close();
        }       ,

echo gmdate("Y-m-d H:i:s", $unix_time)." | ".gmdate("Y-m-d H:i:s", $unix_time2)." "; 

显示:

2010-12-26 01:06:48 | 2010-12-26 02:06:24

I have set my mysql db: SET time_zone = 'America/Los_Angeles';
and in php

date_default_timezone_set('America/Los_Angeles');   

but when I add

$time = strtotime ("+2 minutes");

and

$time2 = gmdate("Y-m-d H:i:s", $time);  

to the db i get wrong an hour wrong when I get it from mysql like this:

        $query ="select UNIX_TIMESTAMP("time"), time2 from tbl; 
        if ($result = $conn->query($query)) {
            while ($row = $result->fetch_assoc()) {
                $unix_time = $row["UNIX_TIMESTAMP("time")"];
                $unix_time2 = $row["time2"];
            }
            $result->close();
        }       ,

echo gmdate("Y-m-d H:i:s", $unix_time)." | ".gmdate("Y-m-d H:i:s", $unix_time2)." "; 

displays:

2010-12-26 01:06:48 | 2010-12-26 02:06:24

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2024-10-16 15:34:18

unix_timestamp() 将检索您的本地时间戳,该时间戳当前处于夏令时模式。 gmdate() 采用格林尼治标准时间(又名 UTC),没有 DST,因此您会得到 1 小时的时差。使用 date() 而不是 gmdate() 来处理 DST 感知。

unix_timestamp() will retrieve your local timestamp, which is currently in daylight savings mode. gmdate() works off Greenwhich Mean Time, aka UTC, which has no DST, so you'll get the 1 hour difference. Use date() instead of gmdate() to work with DST-awareness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文