Forum Moderators: coopster

Message Too Old, No Replies

Date Conversion

         

tchallies

6:12 pm on May 9, 2005 (gmt 0)

10+ Year Member



My database has a standard MYSQL date field, with the format YYYY-MM-DD. How can I convert that to a format like April 15, 2005 in PHP?

kazecoder

8:14 pm on May 9, 2005 (gmt 0)

10+ Year Member



Go here: [php.net...]

$today = date("F j, Y");

tchallies

8:28 pm on May 9, 2005 (gmt 0)

10+ Year Member



Thank you!

tchallies

3:32 pm on May 11, 2005 (gmt 0)

10+ Year Member



I tried this, but now realized I phrased my question poorly.

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?

neophyte

5:02 pm on May 11, 2005 (gmt 0)

10+ Year Member



tchallies -

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.

tchallies

5:32 pm on May 11, 2005 (gmt 0)

10+ Year Member



That worked great. Thanks!