I am rebuilding a web application from an old one with many inconsistencies. I have to migrate all the data over from the old database to our new structure.
In the old database, dates were stored in the MySQL DB as VARCHAR
. We are based in the UK, so dates were written in the format DD/MM/YYYY. I need to convert these dates to MySQL's native DATE()
format.
Problem is this - PHP defaults to assuming the dates are in 'American' format (MM/DD/YYYY) because they were originally split with /
rather than -
- and -
forces PHP to assume they are 'European' format.
I am doing this so far to convert them:
$start_date = date('Y-m-d', strtotime($query->row('startdate')));
Where $query->row('startdate')
is the column in the old database which was storing the dates. Problem is, I need to first switch all the 21/03/1994
s to 21-03-1994
.
How can I do this?
0000-00-00
... – Jack Nov 15 '11 at 12:36%d/%d/%d/
is used, that it isDD/MM/YYYY
? You need to detect first what makes sense, then convert. Probably okay to do this within PHP, but if you can already process a bunch earlier, maybe a two-step strategy is still useful in your case. Own code = own bugs. As long as you stick to existing tools it's most often the best first choice. Defer the details later. See as well dev.mysql.com/doc/refman/5.1/en/regexp.html – hakre Nov 15 '11 at 12:39