在 MySQL 中获取当前 UTC 时间的 unix 时间戳的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17768426/
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
Best way to get unix timestamp for current UTC time in MySQL
提问by littlejedi
I've been trying to use the following in mySQL to create a unix stamp for current UTC time
我一直在尝试在 mySQL 中使用以下内容为当前 UTC 时间创建一个 unix 戳
UNIX_TIMESTAMP(UTC_TIMESTAMP())
When I execute the query and get the result, it seems like mySQL is doing UTC conversation twice.
当我执行查询并获得结果时,似乎 mySQL 进行了两次 UTC 对话。
Eg. local time 9:07PM
Query above returned:
1374390482
, which is next day 07:08:02 GMT
, which is correct,
上面的查询返回:
1374390482
,这是第二天07:08:02 GMT
,这是正确的,
However, UNIX_TIMESTAMP(LOCALTIMESTAMP())
returns:
但是,UNIX_TIMESTAMP(LOCALTIMESTAMP())
返回:
1374372551 02:09:11 GMT
which is the correct UTC unix timestamp.
这是正确的 UTC unix 时间戳。
So UNIX_TIMESTAMP
automatically translates the date object's timezone to UTC (regardless if it's already UTC or not?)
所以UNIX_TIMESTAMP
自动将日期对象的时区转换为 UTC(无论它是否已经是 UTC?)
Is there a better way or a single command just to get a UTC unix timestamp in mySQL?
有没有更好的方法或单个命令来在 mySQL 中获取 UTC unix 时间戳?
回答by Alden
Try just:
试试吧:
UNIX_TIMESTAMP()
And see here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
并在这里看到:http: //dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp