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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.