Hi, Could anyone please help me in accessing Date through 3 drop down boxes (DD/MM/YYYY)? I took 3 list box for Date of Birth MM,DD,and YYYY respectively but unable to store this data into MySQL table in DATE format. Right now, I am using this to store date of birth in database. $day=$_POST['day']; $month=$_POST['month']; $year=$_POST['year']; $dob=$day.'/'.$month.'/'.$year; Could anyone please tell me where I am wrong? Regards, Sumit Kumar
Well if you store the date in MySql date format you need to use the following date format YYYY-MM-DD You can easily convert this using STR_TO_DATE() function of MySql Check this url: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
I wrote this function to convert a date collected by a date picker on the form to a mysql datetime format. you can play around with it to fit your needs. //My datepicker string:Jun 05 2009, 11:54 PM function date_picker_to_mysql($date){ $am_pm = substr($date, -2); $minutes = substr($date, -5, -3); $hours = substr($date, -8, -6); $year = substr($date, -14, -10 ); $day = substr($date, -17, -15); if(string_contains($date, "Dec")) $month = "12"; else if(string_contains($date, "Nov")) $month = "11"; else if(string_contains($date, "Oct")) $month = "10"; else if(string_contains($date, "Sep")) $month = "09"; else if(string_contains($date, "Aug")) $month = "08"; else if(string_contains($date, "Jul")) $month = "07"; else if(string_contains($date, "Jun")) $month = "06"; else if(string_contains($date, "May")) $month = "05"; else if(string_contains($date, "Apr")) $month = "04"; else if(string_contains($date, "Mar")) $month = "03"; else if(string_contains($date, "Feb")) $month = "02"; else if(string_contains($date, "Jan")) $month = "01"; //convert $12hrtime to 24 hour time $hrtime = $hours.":".$minutes." ".$am_pm; $sqlhrtime = date("G:i",strtotime($hrtime)); // Ex. 18:36 if(strlen($sqlhrtime) == 4){ $sqlhrtime = "0".$sqlhrtime; } return $year."-".$month."-".$day." ".$sqlhrtime.":00"; } PHP: enjoy and good luck
Whipped up even shorter code. Does the exact same thing function strtodate($date) { return date("m-d-Y G:i A", strtotime($date)); } print strtodate("January 05 2009, 11:54 PM"); PHP: I think krishmk's method is best though.
to have a valid mysql timestamp your function should be: function strtodate($date) { return date("Y-m-d H:i:s", strtotime($date)); } i meant with my function to have a general procedure that can be manipulated according to a datepicker. Many people have this problem when comming to use a datepicker, the strtotime give them strange results if the $date string is not formated right. for example: for 1-5-2009, the strtotime will say this is january 5th, 2009... but the datepicker could be giving this as may first in some countries. if the string is formated right, i will sure use your function (with the tiny fix i added)
I ran into this too the other day, its usually best practice to store all your dates in MySQL as date format (YYYY-MM-DD) and then use PHP to convert them to another format. Then you can compare dates and use the NOW() function and other date functions in your MySQL queries. ----------------------- Find Online Schools
I agree 100%, otherwise just store it as a varchar, but then it is not useful for any mysql queries. not to mention the flexebility of formating using the php date function