How do I convert the following format to unix timestamp?
Apr 15 2012 12:00AM
The format I get from DB seems to have AM
at the end.
I've tried using the following but it did not work:
CONVERT(DATETIME, Sales.SalesDate, 103) AS DTSALESDATE,
CONVERT(TIMESTAMP, Sales.SalesDate, 103) AS TSSALESDATE
where Sales.SalesDate value is Apr 15 2012 12:00AM
This question is related to
mysql
datetime
timestamp
unix-timestamp
You will certainly have to use both STR_TO_DATE
to convert your date to a MySQL standard date format, and UNIX_TIMESTAMP
to get the timestamp from it.
Given the format of your date, something like
UNIX_TIMESTAMP(STR_TO_DATE(Sales.SalesDate, '%M %e %Y %h:%i%p'))
Will gives you a valid timestamp. Look the STR_TO_DATE
documentation to have more information on the format string.
For current date just use UNIX_TIMESTAMP()
in your MySQL query.
From http://www.epochconverter.com/
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
My bad, SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD. More on using timestamps with MySQL:
http://www.epochconverter.com/programming/mysql-from-unixtime.php
Source: Stackoverflow.com