Page 1 of 1

Small date issue

Posted: Tue May 14, 2002 4:14 pm
by Crashin
I've read the manual on php.net and evidently I'm too tired to get it right now. I've got a user form where the user enters a date in mm/dd/yyyy format. How do I convert that to the format that can be written to my MySQL table, which accepts yyyy/mm/dd? Thanks! :oops:

Posted: Tue May 14, 2002 9:04 pm
by phice
Why not match your form date format, to your MySQL format.

Current Date?

Posted: Tue May 14, 2002 9:20 pm
by pHaZed
If you are trying to retrieve todays date,
just use the date() function.
http://www.php.net search for date in functions,
Otherwise if you want watever date the user enters to be recorded...
It should insert simply as a string, If you make the table type LONGTEXT... etc

Posted: Tue May 14, 2002 9:47 pm
by volka
split the input-field into three: 'day' 'month' and 'year' so you can assemble it as you wish

or rearrange the value of the input-field withsubstr

Posted: Wed May 15, 2002 3:09 am
by mikeq

Code: Select all

list ($month, $day, $year) = split ('ї/.-]', $StartDate);
         $StartDate = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
$StartDate is the variable passed into your script.

This will cope with the following input mm/dd/yyyy mm-dd-yyyy or mm.dd.yyyy

Hope this helps
Mike

Posted: Wed May 15, 2002 9:34 am
by Crashin
Fantastic suggestions! Thank you all so much! I'll start going through them to find the best fit! This forum is the best! :D

Posted: Wed May 15, 2002 12:00 pm
by IndyTim
Something else to consider, once you've settled in on a date conversion, you might want to build it (and other related date conversions etc.) into a user callable function. That way you'll get heavy re-use as your scripting library increases. I have to deal with conversions from MySQL to Unix to_fro EvilEmpire Short Dates etc. Callable functions definitely simplify things.

IndyTim

Posted: Wed May 15, 2002 12:52 pm
by Crashin
That's a great idea, indy! Thanks for the tip!