我正在使用CodeIgniter框架,并使用jQuery和ajax从输入中插入数据。问题是我在ajax中处理日期时发生日期更改。我正在使用MySQL作为数据库,数据类型为Date。
我试图更改日期格式,因为它可能不是相同的格式。
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';
}
}
答案 0 :(得分:0)
1970-01-01
,当MySQL无法将给定日期转换为MySQL中Year-month-day
的日期的特定格式时,MySQL将存储该日期,请使用此代码解决该问题,
function mformat_date($date_string)
{
// Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a
// slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is
// assumed.
return strtotime(implode('-', array_reverse(explode('/', $date_string))));
}
答案 1 :(得分:0)
在插入数据库之前,您应该先尝试一下。
$m = strtr($this->input->post('month'), '/', '-');
然后像这样插入它。
'month' => date('Y-m-d', strtotime($m)),
让我知道结果。