将 FullCalendar 中的事件开始日期更新到我的数据库中导致 00:00:00

Updating event start date from FullCalendar into my database results in 00:00:00

我正在使用 FullCalendar 并尝试在用户在我的日历中拖放事件时更新事件开始时间。这是我使用的附加到 eventDrop 回调的代码 (https://fullcalendar.io/docs/eventDrop):

        alert(info.event.id + ' was dropped on ' + info.event.start);
        $.ajax({
          url: '/Post/dropPost',
          type: 'POST',
          data: { 'postSchedule': info.event.start, 'postId' : info.event.id },
        });
       },

只要警报显示正确的事件 ID 和正确的开始日期(格式如下:2020 年 5 月 5 日,星期二 04:36:00),此方法就有效。 '/Post/dropPost' 方法也被调用并且 postId 变量被正常传递。

但是,当它更新我的数据库时,postSchedule 总是更新为“00:00:00 00:00:00”。

这是我的 dropPost 方法:

    public function dropPost() 
    {
        $data=[];
        $postSchedule = $_POST['postSchedule'];
        $postId = $_POST['postId'];
        $droppedPost = new PostModel;
        $data['id'] = $postId;
        $data['postSchedule'] = $postSchedule;
        $droppedPost->save($data);
    }

通过阅读 FullCalendar 文档,我了解到日期应始终符合 ISO8601 标准:https://fullcalendar.io/docs/date-parsing。那么为什么它不转换到我的数据库中呢。

我数据库中的 postSchedule 列是 DateTime 类型。

我终于找到了答案。我需要在 info.event.start 属性.

上使用 .toISOString()

工作代码如下:

 eventDrop: function(info) {
    alert(info.event.id + ' was dropped on ' + info.event.start);
    $.ajax({
      url: '/Post/dropPost',
      type: 'POST',
      data: { 'postSchedule': info.event.start.toISOString(), 'postId' : info.event.id },
    });
   },

我认为问题在于您没有将 postSchedule 以正确的格式转换为存储在 database 中。

控制器

首先,检查数据是否被 print_r($this->input->post()).
检索,如果是,则将数据转换为正确的形式,即 $data['postSchedule'] = date("Y-m-d H:i:s", strtotime($postSchedule));,然后将数据存储在 database 中,类型为DATETIME
希望对您有所帮助。