MySql - 选择 UTC 格式的时间戳列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7703793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySql - SELECT TimeStamp Column in UTC format
提问by Michel
In my Mysql 5.0 DB I have a column to control LastUpdated information. The Column is a TimeStamp one and MySql automatic updates data.
在我的 Mysql 5.0 DB 中,我有一列来控制 LastUpdated 信息。Column 是一个 TimeStamp 和 MySql 自动更新数据。
I'm trying to select this column in UTC format.
我正在尝试以 UTC 格式选择此列。
The problem is that the server is setup to US datetime. According to MySql documentation, the DB stores the information in UTC but when I want to display information, it converts it to Server's time.
问题是服务器设置为美国日期时间。根据 MySql 文档,数据库以 UTC 格式存储信息,但是当我想显示信息时,它会将其转换为服务器的时间。
Is there any way to do SELECT command avoiding this convertion?
有没有办法做 SELECT 命令来避免这种转换?
采纳答案by fivedigit
Besides changing the default timezone of the server, the timezone can also be adjusted per connection by executing this SQL statement:
除了更改服务器的默认时区外,还可以通过执行以下 SQL 语句来调整每个连接的时区:
SET time_zone = timezone;
Where timezone is the name of the timezone (see MySQL docs).
其中 timezone 是时区的名称(请参阅 MySQL 文档)。
Alternatively, you can also convert a timestamp to a different timezone using the CONVERT_TZ
function.
或者,您也可以使用CONVERT_TZ
函数将时间戳转换为不同的时区。
回答by Timo Huovinen
SELECT
CONVERT_TZ(`timestamp_field`, @@session.time_zone, '+00:00') AS `utc_datetime`
FROM `table_name`
I made a cheatsheet here: Should MySQL have its timezone set to UTC?
我在这里做了一个备忘单:MySQL 是否应该将时区设置为 UTC?