简体   繁体   English

如何仅在mysql中将此日期转换为unix时间戳?

[英]How to convert this date into a unix timestamp in mysql alone?

This is the date: 2018-01-25T13:39:40-05:00 这是日期: 2018-01-25T13:39:40-05:00
Its from an API. 它来自API。

So far, I did this: SELECT UNIX_TIMESTAMP(STR_TO_DATE('2018-01-25T13:39:40-05:00', '%M %d %Y %h:%i%p')) AS time 到目前为止,我做到了: SELECT UNIX_TIMESTAMP(STR_TO_DATE('2018-01-25T13:39:40-05:00', '%M %d %Y %h:%i%p')) AS time

But its giving me NULL . 但是它给了我NULL
I think -05:00 is timezone. 我认为-05:00是时区。 But T is, I don't know. 但是T是,我不知道。

Maybe perhaps there is another way take out T and -05:00 and convert 24hrs to 12hrs 也许还有另一种方法来取出T和-05:00并将24小时转换为12小时

You don't have to use the STR_TO_DATE function. 您不必使用STR_TO_DATE函数。

You can directly use UNIX_TIMESTAMP. 您可以直接使用UNIX_TIMESTAMP。

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年1月1日00:00:00 UTC以来的参数值(以秒为单位)。

The date argument may be a DATE, DATETIME, or TIMESTAMP string, or a number in YYMMDD, YYMMDDHHMMSS, YYYYMMDD, or YYYYMMDDHHMMSS format. date参数可以是DATE,DATETIME或TIMESTAMP字符串,也可以是YYMMDD,YYMMDDHHMMSS,YYYYMMDD或YYYYMMDDHHMMSS格式的数字。

The server interprets date as a value in the current time zone and converts it to an internal value in UTC. 服务器将日期解释为当前时区中的值,并将其转换为UTC中的内部值。 Clients can set their time zone 客户可以设置他们的时区

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp https://dev.mysql.com/doc/refman/5.5/zh-CN/date-and-time-functions.html#function_unix-timestamp

SELECT UNIX_TIMESTAMP('2018-01-25T13:39:40-05:00') AS time

http://sqlfiddle.com/#!9/b2206f/1 http://sqlfiddle.com/#!9/b2206f/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM