如何使用jQuery将日期插入mysql表?

use*_*582 1 php mysql jquery datetime

我正在使用jQuery的datetimepicker,当我从datetimepicker中选择一个日期时,它将它放在以下格式中:

  11/10/2011 14:02
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试将它插入我的sql表中时,就像这样

INSERT INTO MYTABLE (date, name) values ('$_POST[datepicker]','$_POST[name]')
Run Code Online (Sandbox Code Playgroud)

它说日期格式不正确.

有人可以帮我确定如何格式化这样的日期,以便datetime()字段接受它吗?

谢谢

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '14:33:00,1,2,'Y')' at line 2

        $datetosave = date("Y-m-d H:i:s", strtotime($_POST[date]));

$insert_q = "INSERT INTO reservations (res_date, res_numofseats, user_id, rsvp)
                     VALUES ($datetosave,$_POST[cars],2,'Y')";
Run Code Online (Sandbox Code Playgroud)

Nul*_*ion 6

使用YYYY-MM-DD HH:MM:SS格式,例如:2011-11-10 14:02.

理想情况下,您需要设置jQuery插件以正确的格式输出.如果这不可能,您可以通过使用PHP strtotime()date()函数解析日期将其转换为正确的格式:

echo date('Y-m-d H:i:s', strtotime('11/10/2011 14:02')); 
// prints 2011-11-10 14:02:00
Run Code Online (Sandbox Code Playgroud)

参考: MySQL手册上DATETIME