Help with Date fields on a form
Date: 05/18/05
(PHP Community) Keywords: mysql, database, sql, web, google
This is driving me nuts.
I've got about a dozen different forms on my website that all have a date field of some sort, whether it be news date, dob, etc. All of them are used to insert data into a mysql table. I've got the corresponding fields in the table set as "DATE" fields. But I'm having issues getting the date to convert properly.
Since the format of a mysql date field is YYYY-MM-DD, I have to do some converting in cases where I need to retrieve the date from the table. For example, if I'm showing somebody's date of birth, I pull the info from the table and then use date() to convert the date to MM-DD-YYYY. As in $dob = date("m-d-Y", strtotime($dob)); where $dob is the date pulled from the table.
The problem is changing that date and updating the table. When stuffing the date back into the table, I've gotta convert it back to YYYY-MM-DD, so then I use $dob = date("Y-m-d", strtotime($_POST["dob"]));
Works fine unless the date is entered on the form with / instead of - as a separator. So what this leads me to discover is that on forms where the user enters the date, they could enter it any number of ways. Even if I specify that they use dashes instead of slashes, there's no way to guarantee that they will. That means that my date in the table will only be correct some of the time.
So what can I do to the date fields on all of my forms to ensure that no matter how the user enters the date, it will be converted properly to YYYY-MM-DD and stuffed into the table?
The best answer I could find on google was using dropdowns for month, day and year. That would be fine on forms where the date is being entered for the first time. But it would be a huge pain to implement on "edit" forms where the fields are populated from the database when the form loads.
I'm about to start pulling my hair out here, so hopefully somebody has a good idea. I'm open to nearly any suggestion.
Source: http://www.livejournal.com/community/php/299442.html