MySQL date field to readable format.

evildan

Super Moderator
I had been looking for the best way to do this for about a week. This example is very simple, but as I said, for some reason I had a problem finding it. Assuming that some people might be looking for the same thing I have included the code here:

Scenario:
You have a date stored in a MySQL database using the date format. yyyy-mm-dd. You call this date in a PHP page, but you would like the date to appear in a readable format (i.e. January 10, 2004).

Here's how it goes:
Code:
<?
$unformated_date = 2004-01-10;

$part=explode("-",$unformated_date);
$newdate=date("F  jS, Y", mktime(0,0,0,$part[1],$part[2],$part[0]));

echo $newdate;
?>

For a reference to the date formatting options see php.net.

F = A full textual representation of a month, such as January or March
j = Day of the month without leading zeros
S = English ordinal suffix for the day of the month, 2 characters (st, nd, etc)
Y = A full numeric representation of a year, 4 digits

Hope this helps someone as much as it helped me. The final format should look like this:

January 10th, 2004

PS I - of course, tossed this in a function and just call it whenever I come across a date conversion in my scripts.
 
Not very compact is it, though?

Try this:

SELECT DATE_FORMAT ('1975-02-11', '%D %M %Y') AS date
 
Back
Top