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

Incorrect time value to database using datetimepicker input

我在尝试将时间插入 mysql 时遇到此错误。

"Incorrect time value .... "

我的控制器:

$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();

我的观点和javascript:

<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>

jQuery:

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

添加功能:

function add_location(){
branch_open_time = $("#time_range_from").val();
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');
        }

    });
}

时间选择器以 12 小时格式正确显示,我在插入数据库之前将其从 H:i:s 转换为 strtotime,但它不起作用。我做错了什么?

日期时间选择器输入:

万物平等:

将您的代码更新为:

我在变量前面加了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');
     }

 });

}

您日期中的 space 使其在插入前无效

使用它从日期中删除 space 以使其成为有效日期:

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

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 命令之前,您需要 trim 在 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();