将javascript日期正确格式化为MySQL
我正在创建一个基于JavaScript的日历,使用full-calendar.js作为主框架。 因此,为了在MySQL数据库中插入新事件,我有以下代码片段:将javascript日期正确格式化为MySQL,javascript,php,mysql,codeigniter,Javascript,Php,Mysql,Codeigniter,我正在创建一个基于JavaScript的日历,使用full-calendar.js作为主框架。 因此,为了在MySQL数据库中插入新事件,我有以下代码片段: $.post("http://localhost/calendar/index.php/calendar/insert_event", { title : title, start : start, end : end, allDay : allDay
$.post("http://localhost/calendar/index.php/calendar/insert_event",
{
title : title,
start : start,
end : end,
allDay : allDay,
url : ''
},
function(answer) {
console.log(answer);
}
);
开始日期和结束日期只是日期对象:
在calendar.php控制器中,我得到以下输出:
{"title":"lunch",
"start":"Tue Oct 08 2013 08:00:00 GMT-0700 (Pacific Standard Time)",
"end":"Tue Oct 08 2013 08:30:00 GMT-0700 (Pacific Standard Time)",
"allDay":"false",
"url":""}
start和end是MySQL表中的DATETIME类型,其中的列具有与上面相同的类型。
当我使用CodeIgniter的活动记录函数进行插入时,它将插入到表中而不会出现进一步的问题。但是,当我查看MySQL数据库以查看输出时,我看到:
mysql> select * from calendar_utility;
+----+-----+-------+---------------------+---------------------+--------+
| id | url | title | start | end | allday |
+----+-----+-------+---------------------+---------------------+--------+
| 1 | | lunch | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 0 |
+----+-----+-------+---------------------+---------------------+--------+
1 row in set (0.00 sec)
如何正确设置JavaScript日期的格式,以便在MySQL数据库中正确插入?我可能会将JS日期对象转换为符合MySQL日期时间格式的字符串,如下所示:
$.post("http://localhost/calendar/index.php/calendar/insert_event",
{
title : title,
start : start.getFullYear() + "-" + (start.getMonth()+1) + "-" + start.getDate() + " " + start.getHours() + ":" + start.getMinutes() + ":" + start.getSeconds(),
end : end.getFullYear() + "-" (end.getMonth()+1) + "-" + end.getDate() + " " + end.getHours() + ":" + end.getMinutes() + ":" + end.getSeconds(),
allDay : allDay,
url : ''
},
function(answer) {
console.log(answer);
}
);
我可能会将JS日期对象转换为符合MySQL日期时间格式的字符串,如下所示:
$.post("http://localhost/calendar/index.php/calendar/insert_event",
{
title : title,
start : start.getFullYear() + "-" + (start.getMonth()+1) + "-" + start.getDate() + " " + start.getHours() + ":" + start.getMinutes() + ":" + start.getSeconds(),
end : end.getFullYear() + "-" (end.getMonth()+1) + "-" + end.getDate() + " " + end.getHours() + ":" + end.getMinutes() + ":" + end.getSeconds(),
allDay : allDay,
url : ''
},
function(answer) {
console.log(answer);
}
);
请原谅,我错了,strotime没有那么好用:只需将Date对象转换为mysql datetime字符串。这是以前做过的,看看这篇文章:原谅我,我错了,strotime没有那么好用:只需将Date对象转换为mysql datetime字符串。这是以前做过的,看看这篇文章:并不是说-和start.getMonth+1之间缺少+连接符导致了错误。Stackoverflow不允许我编辑少于6个字符的内容。谢谢,我已经更正了。并不是因为-和start.getMonth+1之间缺少+连接符而导致错误。Stackoverflow不允许我编辑少于6个字符。谢谢,我已经更正了。