使用datetimepicker输入到数据库的时间值不正确

时间:2016-10-10 09:32:31

标签: javascript php mysql codeigniter datetimepicker

尝试向mysql插入时间时出现此错误。

“时间值不正确......”

我的控制器:

        //Create the video player and set the video path
        videoPlayerView = (VideoView) rootView.findViewById(R.id.video_area);
        if (videoPlayerView == null) {
            Log.d("VideoPlayer Activity","onCreateView: videoPlayerView is null");
            return null;
        }

        //Set the video URI to the the URL of the video on the server
        videoPlayerView.setVideoURI(yourVideoURL); 

我的观点和javascript:

$data = array(
'branch_open_time'  => date("H:i:s",strtotime($this->input->post('time_range_from'))),
'branch_close_time' => date("H:i:s",strtotime($this->input->post('time_range_to')))
);
$this->db->insert('branch_user',$data);
$this->db->insert_id();

jQuery的:

<div id="datetimepicker" class="input-append" style="resize: none;" data-autoclose="true">
                    <label for="open">Waktu Buka</label><br />
                    <div class="input-group">
                        <input data-format="HH:mm PP" type="text" class="form-control" name="time_range_from" id="time_range_from">
                        <span class="add-on input-group-addon" style="height: 30px">
                        <i class="glyphicon glyphicon-time"></i>
                        </span>
                    </div>
                </div>

<div id="datetimepicker2" class="input-append" style="resize: none;" data-autoclose="true">
                    <label for="open">Waktu Tutup</label><br />
                    <div class="input-group">
                        <input data-format="HH:mm PP" type="text" class="form-control" name="time_range_to" id="time_range_to">
                        <span class="add-on input-group-addon" style="height: 30px">
                        <i class="glyphicon glyphicon-time"></i>
                        </span>
                    </div>
                </div>

添加功能:

$(function() {
    $('#datetimepicker').datetimepicker
        ({
            pick12HourFormat: true,
            language: 'en',
            pickDate: false
        });
    $('#datetimepicker2').datetimepicker
        ({
            pick12HourFormat: true,
            language: 'en',
            pickDate: false
        });
});

时间戳正确显示12小时格式,我在插入数据库之前将其从H:i:s转换为strtotime,但它无效。我做错了什么?

datetimepicker输入:

datetimepicker

2 个答案:

答案 0 :(得分:1)

该行

echo date("H:i:s",strtotime('05 : 54 : 51 PM'));

会产生00:00:00

的结果

然而

echo date("H:i:s",strtotime('05:54:51 PM'));

echo date("H:i:s",strtotime('05:54:51PM'));

会产生17:54:51

的结果

因此,在将数据发送到INSERT命令之前,您需要在javascript或PHP代码中修剪这些空格

例如

$t1 = date('H:i:s', str_replace(' ', '', $this->input->post('time_range_from')));
$t2 = date('H:i:s', str_replace(' ', '', $this->input->post('time_range_to')));
$data = array(
            'branch_open_time'  => $t1,
            'branch_close_time' => $t2
            );
$this->db->insert('branch_user',$data);
$this->db->insert_id();

答案 1 :(得分:-1)

一切都是平等的:

将您的代码更新为:

我在变量前添加了var以将其建立为变量

function add_location(){
   var branch_open_time = $("#time_range_from").val();
   var barnch_close_time = $("#time_range_to").val();

   $.ajax({
     url : 'add_location',
     type: "POST",
     dataType: "text",
     data:{
        time_range_from: branch_open_time,
        time_range_to: barnch_close_time
     },
     success: function(data)
     {     
        location.reload();
     },
     error: function (jqXHR, textStatus, errorThrown)
     {
        alert('Error data');
     }

 });

}

日期中的空格在插入

之前使其无效

使用此选项从日期中删除空格以使其成为有效空间:

str_replace(' ','',$date);