开发者

How to convert human date to unix timestamp in Mysql?

开发者 https://www.devze.com 2023-04-13 03:11 出处:网络
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

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.

Is there any function in mysql which converts human date to unix timestamp insi开发者_如何学运维de query itself?


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/


UNIX_TIMESTAMP() Should do the trick!

From MySQL Docs:

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.

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


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


Yes. SELECT UNIX_TIMESTAMP(column) FROM TABLE


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号