php日期错误,php – MySQL日期插入错误

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值