如何将要保存的日期差异的结果保存到数据库中?
public function save(){
$item = $this->input->post('item');
$date_in = $this->input->post('date_in'); //date in
$date_out = $this->input->post('date_out'); // date out
$hasil = $date_in->diff($date_out);
//echo $hasil->format('%a'); I will save these results to the database
$data_insert = array(
'item' => $item,
'date_in' => $date_in,
'date_out' => $date_out,
'selisih' => $hasil
);
//url save
}
字符串没有方法diff
,diff
是日期时间的方法,则需要将字符串更改为日期时间。
它将返回日期间隔对象,您可以按格式('%a'(获取总天数并插入到数据库;
$hasil = (new DateTime($date_in))->diff(new DateTime($date_out));
$hasil = $hasil->format('%a');
$data_insert = array(
'item' => $item,
'date_in' => $date_in,
'date_out' => $date_out,
'selisih' => $hasil
);
$this->db->insert('mytable', $data_insert);
如果你想得到差异,你可以使用Carbon轻松地获得它diffInDays
示例代码为你:
$first = CarbonCarbon::createFromFormat('Y-m-d H:s:i', $date_in);
$second = CarbonCarbon::createFromFormat('Y-m-d H:s:i', $date_out);
$diff_in_days = $second->diffInDays($first);
或者没有碳:
$first = new DateTime($date_in);
$second = new DateTime($date_out);
$interval = $first->diff($second);