Manual insert date into mysql

newbie so sorry.

I have a form that I would like to input a date and have it inserted into mysql db.

so my form has:

<input type=“date” name=“date”>

my .php file has:

$query=“INSERT INTO table (date,user) VALUES (‘$_POST[date]’,‘$_POST[user]’)”;

But when I use my form if I type 1/1/2004, in mysql I get something like 2001-08-20.

How do I format the input from the form to be inserted in the correct format into mysql?

Thanks much!

you must make sure that the date is always submitted to mysql in year-month-day sequence

your options are:

  • a string 'YYYY-MM-DD ’
  • a string ‘YY-MM-DD’
  • a string ‘YYYYMMDD’
  • a string ‘YYMMDD’
  • a number YYYYMMDD
  • a number YYMMDD

You can format the date:

$myDate = explode('/', $_POST['date']);
$myDate = $myDate[2]."-".$myDate[1]."-".$myDate[0];

I usually make a variable to insert into the database:

$dbdate = date("Y-m-d");

Then I add it to my query:

$query="INSERT INTO table (date,user) VALUES ('$dbdate','$_POST[user]')";

When I pull it from the database for display I explode it and format it:

  $arrdate = explode("-",$row['date']); 
  $formatteddate = date("D, F d Y", mktime(0,0,0,$arrdate[1],$arrdate[2],$arrdate[0]));

That gives me something like: [b]Wed November 10, 2004

[/b]Make sense?

If you are using the date to log when the form was submitted, you could use…

$query="INSERT INTO table (date,user) VALUES (NOW(),'$_POST[user]')";

That would put todays date into your table.


Cool, that is exactly what I wanted.

Thanks much!