When you storing DATE in MySQL, the default DATE datatype format is YYYY-MM-DD
For example: 2006-05-20
How to convert this to '20 May 2006' in PHP?
| SitePoint Sponsor |


When you storing DATE in MySQL, the default DATE datatype format is YYYY-MM-DD
For example: 2006-05-20
How to convert this to '20 May 2006' in PHP?
[Home Sweet Home]
http://uk2.php.net/manual/en/function.explode.php - php explode functionPHP Code:// where $date is the date you wish to convert
// first convert to a timestamp
list($year, $month, $day) = explode('-', $date);
$timestamp = mktime(0, 0, 0, $month, $day, $year);
// then convert to a date
$converted = date('j F Y', $timestamp);
http://www.php.net/manual/en/function.mktime.php - php mktime function
http://www.php.net/manual/en/function.date.php - php date function
Hope this helps!![]()
www.supremewebsolutions.com
Aberdeen based web design.
Hotel booking systems - Content management systems
CMS - Search engine optimisation - SEO
You can also do it right when it comes out of MySQL using date_format:
Code MySQL:SELECT date_format(field_name, '%d %M %Y'), other_field_name, maybe_another_field FROM table_name




For mysql:PHP Code:echo date('d M Y');
PHP Code:$date = $row->date;
echo date('d M Y',strtotime($date));
Check here: us2.php.net/manual/en/function.strtotime.php
(there are many date/time related functions in PHP - you should surf around at php.net)
Or, if you intend to do math or otherwise manipulate the m/d/y pieces of the date, try this:
list($y, $m, $d) = explode($StoredDate, "-");
[sorry for duplicating the answers of others - I type too slow, I guess.]
Last edited by eLIANT; Oct 10, 2009 at 11:10. Reason: Too slow...

To decide where to do the conversion to the date format you want.
If it is a really simple application where you are extracting the date and displaying it straight away then use the SQL command to return the date in the format you are going to display it in.
If the application is more involved then process the date throughout in the ccyy-mm-dd format that it gets returned in and have convert it to the desired display format when processing the template file to output the page.
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
There's a huge detailed list in PHP.net, check that list. You can provide any date format with true codes.
Bookmarks