首页 > 解决方案 > 使用ajax处理将日期插入数据库时​​日期更改(CodeIgniter)

问题描述

我正在使用 CodeIgniter 框架并使用 jQuery 和 ajax 从输入中插入数据。问题是当我在 ajax 中处理它时日期会发生变化。我使用 MySQL 作为数据库,数据类型是日期。

我试图更改日期的格式,因为它可能不是相同的格式。

jQuery/Ajax:

    $(document).on('submit', '#insertQL_form', function(event){
      event.preventDefault();
      var month = $('#datetimepicker1').val();
      var nameCompany = $('#nameCompany').val();
      var email = $('#email').val();
      var position = $('#position').val();
      var ojLink = $('#ojLink').val();
      var remarks = $('#remarks').val();
      var withTestTask = $('#withTestTask').val();
      var testTaskStatus = $('#testTaskStatus').val();
      var withInterview = $('#withInterview').val();
      var overallStatus = $('#overallStatus').val();
      alert(month);
      $.ajax({
        url:"<?php echo base_url() . 'home/insertQL'?>",
        method:'POST',
        data: new FormData(this),
        contentType:false,
        processData:false,
        success:function(data)
        {
            alert(data);        
        }
      });
    });

控制器功能:

    public function insertQL()
    {
         if($_POST["action"] == "Add")
         {
            $insert_data = array(
                'month' =>  date("Y-m-d",$this->input->post('month')),
                'nameCompany' => $this->input->post('nameCompany'),
                'email' => $this->input->post('email'),
                'position' => $this->input->post('position'),
                'ojLink' => $this->input->post('ojLink'),
                'remarks' => $this->input->post('remarks'),
                'withTestTask' => $this->input->post('withTestTask'),
                'testTaskStatus' => $this->input->post('testTaskStatus'),
                'withInterview' => $this->input->post('withInterview'),
                'overallStatus' => $this->input->post('overallStatus')
            );
            $this->load->model('QLModel');
            $this->QLModel->insert_crud($insert_data);
            echo 'Data Inserted';
         }
    }

标签: phpjqueryajaxcodeigniter

解决方案


您应该在插入数据库之前尝试此操作。

$m = strtr($this->input->post('month'), '/', '-');

然后像这样插入它。

'month' =>  date('Y-m-d', strtotime($m)),

让我知道结果。


推荐阅读