PHP Convert Text Date to MySQL Date
Posted by: david on 06/30/2010 - 07:49 PM
In PHP, let's say you want to convert the text string "July 1, 2010" to the MySQL date format, you would use two functions- strftime() and strtotime().
strftime() takes 2 arguments- first is the date format, and for MySQL our format would be: '%Y-%m-%d'. The second argument is the integer timestamp- that's where the "magical" function strtotime() comes in. It will take a date text string and convert it into the timestamp integer value we need.
Thus, to convert "July 1, 2010" to "2010-07-01", the PHP code would be:
$mysql_date = strftime('%Y-%m-%d', strtotime('July 1, 2010'));
strftime() takes 2 arguments- first is the date format, and for MySQL our format would be: '%Y-%m-%d'. The second argument is the integer timestamp- that's where the "magical" function strtotime() comes in. It will take a date text string and convert it into the timestamp integer value we need.
Thus, to convert "July 1, 2010" to "2010-07-01", the PHP code would be:
$mysql_date = strftime('%Y-%m-%d', strtotime('July 1, 2010'));