In a serious oversight on my part, I have sorted all date formats as %M %d %Y (July 3 2012)
rather than %Y %m %d (2012 07 03)
.
I want to convert all of the dates of a custom field (called ‘game_release_date’) to the new format.
The code I’ve got so far (but doesn’t work) is:
UPDATE `wp_postmeta`
SET meta_key = DATE(STR_TO_DATE(game_release_date, '%m %d %Y'))
WHERE DATE(STR_TO_DATE(game_release_date, '%M %d %Y'));
To convert the string containing
July 03 2012
into a MySQL date, use MySQL’sSTR_TO_DATE()
function:I’d recommend storing the result of this function in a column of the
DATE
datatype.To convert a MySQL date (whether the result of the above function, or a
DATE
column) into2012 07 03
, use MySQL’sDATE_FORMAT()
function:Therefore, if you want to keep your dates in a string (generally a bad idea) but in the new format: