PHP mysql insert date format
Answer a question
Im using jQuery datepicker the format of the datepicker is this 08/25/2012
i have errors when inserting to my database it insert only 0000-00-00 00 00 00
my codes is
<?php
$id = $_POST['id'];
$name = $_POST['name'];
$date = $_POST['date'];
$sql = mysql_query( "INSERT INTO user_date VALUE( '', '$name', '$date')" ) or die ( mysql_error() );
echo 'insert successful';
?>
im sure my insert is correct....
Answers
As stated in Date and Time Literals:
MySQL recognizes
DATEvalues 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
YYYYMMDDorYYMMDDformat, provided that the number makes sense as a date. For example,19830905and830905are interpreted as'1983-09-05'.
Therefore, the string '08/25/2012' is not a valid MySQL date literal. You have four options (in some vague order of preference, without any further information of your requirements):
-
Configure Datepicker to provide dates in a supported format using an
altFieldtogether with itsaltFormatoption:<input type="hidden" id="actualDate" name="actualDate"/>$( "selector" ).datepicker({ altField : "#actualDate" altFormat: "yyyy-mm-dd" });Or, if you're happy for users to see the date in
YYYY-MM-DDformat, simply set thedateFormatoption instead:$( "selector" ).datepicker({ dateFormat: "yyyy-mm-dd" }); -
Use MySQL's
STR_TO_DATE()function to convert the string:INSERT INTO user_date VALUES ('', '$name', STR_TO_DATE('$date', '%m/%d/%Y')) -
Convert the string received from jQuery into something that PHP understands as a date, such as a
DateTimeobject:$dt = \DateTime::createFromFormat('m/d/Y', $_POST['date']);and then either:
-
obtain a suitable formatted string:
$date = $dt->format('Y-m-d'); -
obtain the UNIX timestamp:
$timestamp = $dt->getTimestamp();which is then passed directly to MySQL's
FROM_UNIXTIME()function:INSERT INTO user_date VALUES ('', '$name', FROM_UNIXTIME($timestamp))
-
-
Manually manipulate the string into a valid literal:
$parts = explode('/', $_POST['date']); $date = "$parts[2]-$parts[0]-$parts[1]";
Warning
-
Your code is vulnerable to SQL injection. You really should be using prepared statements, into which you pass your variables as parameters that do not get evaluated for SQL. If you don't know what I'm talking about, or how to fix it, read the story of Bobby Tables.
-
Also, as stated in the introduction to the PHP manual chapter on the
mysql_*functions:This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
-
You appear to be using either a
DATETIMEorTIMESTAMPcolumn for holding a date value; I recommend you consider using MySQL'sDATEtype instead. As explained in TheDATE,DATETIME, andTIMESTAMPTypes:The
DATEtype is used for values with a date part but no time part. MySQL retrieves and displays DATE values in'YYYY-MM-DD'format. The supported range is'1000-01-01'to'9999-12-31'.The
DATETIMEtype is used for values that contain both date and time parts. MySQL retrieves and displaysDATETIMEvalues in'YYYY-MM-DD HH:MM:SS'format. The supported range is'1000-01-01 00:00:00'to'9999-12-31 23:59:59'.The
TIMESTAMPdata type is used for values that contain both date and time parts.TIMESTAMPhas a range of'1970-01-01 00:00:01'UTC to'2038-01-19 03:14:07'UTC.
更多推荐



所有评论(0)