Posted By

daveismyname on 09/28/09


Tagged

mysql date format


Versions (?)

Date format for mysql input


 / Published in: PHP
 

This snippet collects a date from a var and formats it into the correct format for database date entry

  1. //date from a form in the format of dd/mm/yyyy
  2.  
  3. $date_array = explode("/",$date); // split the array
  4. $var_day = $date_array[0];//day seqment
  5. $var_month = $date_array[1];//month segment
  6. $var_year = $date_array[2];//year segment
  7. $departure = "$var_year-$var_day-$var_month";//join them all together to input into a mysql table

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: daveismyname on September 28, 2009

Spotted a mistake the last line should be: $date= "$varyear-$varday-$var_month";//join them all together to input into a mysql table

Posted By: nkm on September 28, 2009

MySQL date format is yyyy-mm-dd. Also, you should check if the date is correct.

    
function date_mysql($str)
{
    list($day, $month, $year) = explode('/', $str);

if (checkdate((int) $month, (int) $day, (int) $year))
{
    return $year.'-'.$month.'-'.$day;
}

return FALSE;

}

You need to login to post a comment.