Convert MySQL date time 24 hour format to UNIX timestamp

Viewed 449

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

1 Answers

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.

Related