I'm looking for a solution to convert MM/DD/YYYY HH:MM:SS stored dates, in a MySQL table, to Unix timestamps.
Asked
Active
Viewed 2,747 times
0
-
3Your first problem is that the datetimes are stored as strings. – JJJ Aug 14 '13 at 12:20
-
2Is it too late to start storing dates as dates? – Álvaro González Aug 14 '13 at 12:22
-
@ÁlvaroG.Vicario Unfortunately, I have no control of the data until it's imported - it's all generated from "ancient" technology. – Aug 14 '13 at 12:54
1 Answers
1
Since your date is in an odd format you can convert it using STR_TO_DATE
then use UNIX_TIMESTAMP
.
SELECT UNIX_TIMESTAMP(STR_TO_DATE('08/14/2013 22:12:19','%m/%e/%Y %H:%i:%s'));
You may want to consider switching the stored dates to true dates as this will make using the MySQL date functions simpler.

Jim
- 22,354
- 6
- 52
- 80
-
Unfortunately I have no control over the date format until it's imported into the database. What would you suggest as the most efficient solution to converting all of the dates in the table (several million), putting the valid dates into a neighbouring column? (or worst case over-writing the existing). – Aug 14 '13 at 12:44
-
@eversonjw I would put the dates into a new column and move over the existing, that way if anything goes wrong the old column will still be there. As for efficiency it's perhaps better to ask that as a new question so you can get the best answer. – Jim Aug 14 '13 at 12:53