views:

36

answers:

2

I have a MySQL varchar column full with dates stored in dd-M-yyyy format. E.g:

Row 1: 12-jan-2010
Row 2: 23-jun-2016

What's the best way to convert this to mySQL datetime format using php?

+1  A: 

First, make sure your timezone is UTC: see date_default_timezone_set().

Use strtotime() to convert the existing string to a UNIX timestamp.

Use date() to convert it back to one or another standard format, e.g. Y-m-d H:i:s.

Put it back into MySQL.

kijin
+5  A: 

Can be done without using php:

update myTable set newcol = STR_TO_DATE(oldcol, '%d-%M-%Y')
bemace
+1 No need to go through the trouble to `SELECT`, convert in PHP then `UPDATE` for every row.
BoltClock
Bingo! Thank you, this did the trick nicely.
Michael Pasqualone