将MySQL日期时间24小时格式转换为UNIX时间戳

时间:2020-03-04 07:17:13

标签: mysql

我将日期时间以这种格式存储在04-09-2019 10:31:AM(2019年9月4日上午10:31)中。

我可以知道如何在sql查询中将此格式转换为unixtime

1 个答案:

答案 0 :(得分:3)

首先,您需要使用str_to_date()函数将其转换为正确的 mysql 日期时间。

str_to_date(dd, '%d-%m-%Y %h:%i:%p')

然后在获得正确的 datetime 值之后,使用unix_timestamp()函数将其转换为Unix时间戳。

select 
    unix_timestamp(str_to_date(dd, '%d-%m-%Y %h:%i:%p'))
from test

尝试dbfiddle