Convert MySQL date time 24 hour format to UNIX timestamp

Bijesh Samani :

I'm storing date time in 04-09-2019 10:31:AM(4 September 2019 10:31 AM) in this format in MySQL table.

May I know how to convert this format to unixtime stamp in sql query

Ed Bangga :

First you need to convert this into a proper mysql datetime using str_to_date() function.

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

Then after getting the correct datetime value, use unix_timestamp() function to convert it to unix timestamp.

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

try this dbfiddle.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25539&siteId=1