Jump to content

[SOLVED] Formatting date


link7722

Recommended Posts

Thank you for your answer but it seems that when i put 24/12/2008 (dd/mm/yyyy) in the textbox assign it to a variable like $date4 = 24/12/2008 ,after using

date("Y-m-d",strtotime($date4)); 

i get the date like 2008-24-12(yyyy-dd-mm) and not like 2008-12-24(yyyy-mm-dd) as i would like.

Thank you

Sometomes strtotime is not good enough (it will not recognize this date format).

 

Try this

$date = explode("/","24/12/2008");
$dateReformatted = "{$date[2]}-{$date[1]}-{$date[0]}";
echo $dateReformatted;

 

AFAIR For inserting to MySQL you could as well use

$dateReformatted = $date[2].$date[1].$date[0];

 

(as long as year is always 4 digits, and both month and day are always two digits)

dd/mm/yyyy is not a format that strtotime() understands (and would give 1969-12-31 using the posted code.) It would need to be mm/dd/yyyy to work.

 

However, as Mchl posted, dd-mm-yyyy is a format that strtotime() has been programmed to parse.

 

There is (used to be) a link on the strtotime page in the manual that lists what formats strtotime works with.

Here is the original source documentation of the strtotime() supported formats - http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html#SEC114

 

The dd-mm-yyyy format is not officially listed (and is not what Mchl was using since I misread what he posted) but works as long as a 4 digit year is used.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.