PDA

View Full Version : Converting text input european date and inserting into mysql


dprichard
04-30-2008, 02:11 PM
I have a client that wants to have the dates formatted dd-mm-yyyy. I have a text input box and put the date in, but when I try to insert it, when I look at the record it shows 0000-00-00. I am trying to figure out how to convert the date so mysql will accept it, but am not having any luck. Any assistance would be greatly appreciated.

Thank you!

abduraooft
04-30-2008, 02:16 PM
You may have to use substr() (http://www.php.net/substr) to strip this and then combine in to the mysq'sl datetime format.

PS: http://www.dynamicdrive.com/dynamicindex7/jasoncalendar.htm is a very useful and easy calendar script.

CFMaBiSmAd
04-30-2008, 02:23 PM
Also, the mysql STR_TO_DATE() function will convert any format into a standard DATE format in your query (as always with any method, before doing anything with user supplied input, validate it to make sure it is set, it is of the expected format, and it is a valid date in the expected format.)

dprichard
04-30-2008, 02:48 PM
Thank you both for your help. The STR_TO_DATE wasn't converting 01-20-2008 for me very well. I would have blank value when I looked into my database.

I ended up using the substr() and converting it like this.


$emp_hire_date_d = substr($emp_hire_date, 0, -8);
$emp_hire_date_m = substr($emp_hire_date, 3, -5);
$emp_hire_date_y = substr($emp_hire_date, -4);

$emp_hire_date = $emp_hire_date_y.'-'.$emp_hire_date_m.'-'.$emp_hire_date_d;


Thank you again to everyone for your help.

abduraooft
04-30-2008, 02:56 PM
The STR_TO_DATE wasn't converting 01-20-2008 for me very well. I would have blank value when I looked into my database. Please show your code for this.

dprichard
04-30-2008, 03:27 PM
My apologies. It was my code. I got it working both ways.