link7722 Posted January 17, 2009 Share Posted January 17, 2009 I am trying to find a way (a function would be preferred) to change the format of a date which is displayed like dd/mm/yyyy to yyyy-mm-dd so i can insert it in mysql date column. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/141189-solved-formatting-date/ Share on other sites More sharing options...
RussellReal Posted January 17, 2009 Share Posted January 17, 2009 date("Y-m-d",strtotime("10/5/1990")); Quote Link to comment https://forums.phpfreaks.com/topic/141189-solved-formatting-date/#findComment-738957 Share on other sites More sharing options...
link7722 Posted January 17, 2009 Author Share Posted January 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141189-solved-formatting-date/#findComment-738981 Share on other sites More sharing options...
Mchl Posted January 17, 2009 Share Posted January 17, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/141189-solved-formatting-date/#findComment-738983 Share on other sites More sharing options...
PFMaBiSmAd Posted January 17, 2009 Share Posted January 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141189-solved-formatting-date/#findComment-738985 Share on other sites More sharing options...
PFMaBiSmAd Posted January 17, 2009 Share Posted January 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141189-solved-formatting-date/#findComment-738989 Share on other sites More sharing options...
link7722 Posted January 17, 2009 Author Share Posted January 17, 2009 Thank you for your answers.Problem solved with explode() Quote Link to comment https://forums.phpfreaks.com/topic/141189-solved-formatting-date/#findComment-739332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.