Page 1 of 1

Format A Date... quick question

Posted: Wed Apr 05, 2006 1:21 pm
by seodevhead
Hey guys... I have a database that has dates stored in the following format:

2006-04-05

Is there anyway in PHP to convert this to April 5, 2006? Even better, knock off the year and just get April 5? Thanks for your help!

Posted: Wed Apr 05, 2006 1:31 pm
by feyd
You could use the databases' own functionality such at MySQL's DATE_FORMAT function.

For PHP to handle it correctly, explode() it then pass the data to mktime() and finally that result through date() in the format string you wish.

Posted: Wed Apr 05, 2006 2:15 pm
by irisblaze

Code: Select all

<?php
$timestamp = strtotime("2006-04-05");
//print date as April 05, 2006
echo date("F d, Y", $timestamp);
echo "<br/>";
//print date as April 05
echo date("F d", $timestamp);
?>
Now it should work

Posted: Wed Apr 05, 2006 2:17 pm
by feyd
unfortunately, strtotime() may read the date wrong, depending on locale and other things, so be very careful using it in this case.

Posted: Wed Apr 05, 2006 2:25 pm
by irisblaze
feyd wrote:unfortunately, strtotime() may read the date wrong, depending on locale and other things, so be very careful using it in this case.

not if you pass the date in the right format!

I mean let's say that you passed the string as 2006-12-20, does strtotime returns time stamp for the date 2006-20-12 depending on the locale?