drnpwmq4536 2017-04-17 11:51
浏览 50
已采纳

DateTime从FullCalendar转换为Controller PHP

I'm using FullCalendar to display a calendar on the web page (no surprise there).
A user can add events on the calendar, which is then passed through POST to the CodeIgniter controller. The controller executes a function in the model to insert the data in the database.

The problem I'm facing is that the date time, when inserted in the database adds 4 minutes for a reason i do not understand.

This is the JavaScript used for FullCalendar:

$(document).ready(function() {
    var today = moment().day();
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        editable: true,
        defaultView: 'agendaWeek',
        eventLimit: true, // allow "more" link when too many events
        selectable: true,
        selectHelper: true,
        select: function(start, end) {
            var title = "<?=$anim['NOMANIM']?>";
            var id = "<?=$anim['CODEANIM']?>";
            if (title) {
                start   =   moment(start).format('YYYY-MM-DD HH:mm:ss');
                end     =   moment(end).format('YYYY-MM-DD HH:mm:ss');
                $.ajax({
                    url: '<?php echo base_url("index.php/activite/add_activite"); ?>',
                    data: 'title='+ title+'&id='+ id+'&start='+ start +'&end='+ end,
                    type: "POST",
                    success: function(json) {
                        alert(start); // THIS SHOWS THE CORRECT DATETIME
                    }
                })
            };
            $('#calendar').fullCalendar('renderEvent',
            {
                title: title,
                start: start,
                end: end,
            }, true); // stick? = true
            $('#calendar').fullCalendar('unselect');
        },
        events: <?=$events?>
    });
});

And here is the controller function:

public function add_activite() {
    $title = $_POST['title'];
    $id = $_POST['id'];

    $datetime_start = strtotime($_POST['start']);
    $datetime_end   = strtotime($_POST['end']);

    $date   = date('Y-m-d', $datetime_start);
    $start  = date('H:m:s', $datetime_start);
    $end    = date('H:m:s', $datetime_end);
    $this->EZ_query->insert_activite($date, $start, $end);
}

If you think there is something I'm doing not correctly beside the time problem, I'm open to any pointers :), Thanks!

  • 写回答

1条回答 默认 最新

  • duanpo1498 2017-04-17 11:55
    关注

    Your data formatting in php is not correct, minutes are denoted by i not m (which is used my month already). See here: http://php.net/manual/en/function.date.php

    So change:

    $start  = date('H:i:s', $datetime_start);
    $end    = date('H:i:s', $datetime_end);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测