updating a date type in mysql with php

I have a date field in mysql table (dueDate) . I have tried to update that column in this way: I have a function that receives 4 parameters: the taskId - to identify the row, and the parameters of the new date: year,month,day.

 function editTask($taskId,$year,$month,$day){
      $str= $year.'-'.$month.'-'.$day;
      $date=date('Y-m-d', strtotime($str));
      $result = mysql_query("UPDATE tasks SET dueDate='$date' WHERE taskId = $taskId");
 }

but it changes the date collumn to be 0-0-0.

why isn't working ?

after adding the quoting it changes the date to be 1969-12-31 instead of 0-0-0. does anyone have any idea why?

Answers


MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. So you have to enclose the date within quotes. Also you are creating a string from the date values. So there is no need of using strtotime(). Try like

$str= $year.'-'.$month.'-'.$day;
$result = mysql_query("UPDATE tasks SET dueDate='$str' WHERE taskId = $taskId");

Note:

MySQL recognizes DATE values in these formats:

  • As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31', '2012/12/31', '2012^12^31', and '2012@12@31' are equivalent.

  • As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date. For example, '20070523' and '070523' are interpreted as '2007-05-23', but '071332' is illegal (it has nonsensical month and day parts) and becomes '0000-00-00'.

  • As a number in either YYYYMMDD or YYMMDD format, provided that the number makes sense as a date. For example, 19830905 and 830905 are interpreted as '1983-09-05'.


Need Your Help

c# creating tabs within a form

c# tabs

So basically my issue is that I have a windows form application that graphs data that is read in. The problem is that if I want to generate two, three or any number of graphs with different data se...