如
Date and Time Literals所述:
MySQL recognizes 07001 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'.
因此,字符串’12 -Aug-2012’不是有效的MySQL日期文字.您有三种选择(在一些模糊的优先顺序中,没有任何关于您的要求的进一步信息):
>配置Datepicker以使用其dateFormat option以受支持的格式提供日期:
$( ".selector" ).datepicker({ dateFormat: "yyyy-mm-dd" });
>使用MySQL的STR_TO_DATE()函数转换字符串:
opp_deadline=STR_TO_DATE('$_POST[deadline]', '%d-%b-%Y')
注意:请参阅下面有关SQL注入的警告.
>将收到的字符串转换为PHP时间戳 – 例如.目前使用strtotime():
$timestamp = strtotime($_POST['deadline']);
然后是:
>使用date()格式化时间戳:
$date = date('Y-m-d', $timestamp);
>使用FROM_UNIXTIME()将时间戳直接传递给MySQL:
opp_deadline=FROM_UNIXTIME('$timestamp')
警告
>您的代码易受SQL注入攻击.您真的应该使用prepared statements,将变量作为参数传递给SQL,这些参数不会被SQL评估.如果您不知道我在说什么,或者如何解决它,请阅读Bobby Tables的故事.
>另外,如the introduction所述,关于mysql_ *函数的PHP手册章节:
This extension is not recommended for writing new code. Instead, either the 070010 or 070011 extension should be used. See also the 070012 for further help while choosing a MySQL API.