如何在 MySQL 中将 DateTime 转换为数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1921574/
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
How to convert DateTime to a number in MySQL?
提问by Jader Dias
How can I get the total number of seconds since '1970-01-01 00:00:01'
from a DateTime instance in MySQL?
如何'1970-01-01 00:00:01'
从 MySQL 中的 DateTime 实例获取自以来的总秒数?
回答by Sven Lilienthal
You are looking for UNIX_TIMESTAMP()
.
您正在寻找UNIX_TIMESTAMP()
.
See: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp
请参阅:http: //dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp
If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC.
如果使用日期参数调用 UNIX_TIMESTAMP(),它会返回参数值作为自 '1970-01-01 00:00:00' UTC 以来的秒数。
回答by martin clayton
回答by Tebo
SELECT DATE_FORMAT(`value`, '%Y%m%d') AS `date_ymd` FROM `table_name`;
回答by Brethlosze
UNIX_TIMESTAMP(datetime)
force a localization of the datetime, which unlike the timestamp, is stored "as is".
UNIX_TIMESTAMP(datetime)
强制日期时间的本地化,与时间戳不同,它“按原样”存储。
You need actually any of the following, for discarding the UTC correction:
您实际上需要以下任何一项,以丢弃 UTC 更正:
UNIX_TIMESTAMP(CONVERT_TZ(datetime, '+00:00', @@session.time_zone))
UNIX_TIMESTAMP(CONVERT_TZ(datetime, '+00:00', @@session.time_zone))
or:
或者:
TIMESTAMPDIFF(SECOND,'1970-01-01 00:00:00',datetime)
TIMESTAMPDIFF(SECOND,'1970-01-01 00:00:00',datetime)