简体   繁体   中英

How to convert human date to unix timestamp in Mysql?

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 inside 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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