Forum Moderators: coopster
I currently am pulling a MYSQL DATE field in the standard format of YYYY-MM-DD. I would like to take this and convert it into something more useful.
This code: $today = date("F j, Y"); will not convert a variable, but will provide today's date. So how do I take a MYSQL date such as 2005-05-11 and convert it to May 11, 2005?
I just went through this myself and, thanks to a few people here at WW, found the answer was to use the mySQL function date_format() within your SELECT query.
In your SELECT query, do this:
$query = ("SELECT name, date_format(date_field,'%W, %M %D, %Y.') FROM guest ORDER BY id DESC");
(date_field is the field assigned datatype "date" in your DB table)
The date_format function above converts a regular-old YYYY-MM-DD format into "Thursday, May 11th, 2005" as it's drawn into your recordset.
You can find all the different conversion codes (i.e. %W, %M) on the mySQL site under Date and time functions.
Make sure to put double quotes before and directly after your select statement (as shown above), or the single quotes inside the date_format function will throw an error.