使用 UNIX_TIMESTAMP 和 time() 时出现一个小时错误
我已经设置了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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. Usedate()
instead ofgmdate()
to work with DST-awareness.