MySQL 如何在Mysql中将人类日期转换为unix时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7763758/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:20:55  来源:igfitidea点击:

How to convert human date to unix timestamp in Mysql?

mysqldate

提问by developer

I have a table with a date field, having human date in it like: '2008-01-08 19:23:32' Now i have to copy this field plus some other fields of the same table to another table, but date needs to be in unix timestamp.

我有一个带有日期字段的表,其中包含人工日期,例如:'2008-01-08 19:23:32' 现在我必须将此字段以及同一表的其他一些字段复制到另一个表中,但需要日期在unix时间戳中。

Is there any function in mysql which converts human date to unix timestamp inside query itself?

mysql 中是否有任何函数可以将人类日期转换为查询本身内的 unix 时间戳?

回答by jcomeau_ictx

mysql> select unix_timestamp('2008-01-08 19:23:32');
+---------------------------------------+
| unix_timestamp('2008-01-08 19:23:32') |
+---------------------------------------+
|                            1199849012 |
+---------------------------------------+
1 row in set (0.04 sec)

found here: http://www.epochconverter.com/

在这里找到:http: //www.epochconverter.com/

回答by developer

UNIX_TIMESTAMP()Should do the trick!

UNIX_TIMESTAMP()应该做的伎俩!

From MySQL Docs:

来自 MySQL 文档:

If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer. 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. date may be a DATE string, a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or YYYYMMDD. The server interprets date as a value in the current time zone and converts it to an internal value in UTC.

如果不带参数调用,则返回 Unix 时间戳(自 '1970-01-01 00:00:00' UTC 以来的秒数)作为无符号整数。如果使用日期参数调用 UNIX_TIMESTAMP(),它会返回参数值作为自 '1970-01-01 00:00:00' UTC 以来的秒数。date 可以是 DATE 字符串、DATETIME 字符串、TIMESTAMP 或格式为 YYMMDD 或 YYYYMMDD 的数字。服务器将日期解释为当前时区中的值,并将其转换为 UTC 中的内部值。

mysql> SELECT UNIX_TIMESTAMP();
        -> 1196440210
mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
        -> 1196440219

回答by xdazz

SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');

回答by gview

Yes. SELECT UNIX_TIMESTAMP(column) FROM TABLE

是的。 SELECT UNIX_TIMESTAMP(column) FROM TABLE

回答by Diego Favero

Query:

询问:

SELECT UNIX_TIMESTAMP(TIMESTAMP(`opened`)) as timestamp_date, `opened` as datetime_type FROM `myterminal`

Outputs:

输出:

| timestamp_date        | datetime_type     
|-------------------    |---------------------
| 1536602012            | 2018-09-10 14:53:32
| 1536603854            | 2018-09-10 15:24:14