在codeigniter中找到与数据库的两个日期时间差异

时间:2017-09-17 15:10:23

标签: javascript php mysql codeigniter datetime

我有3个输入表单,它将数据提交并保存到数据库,表格为:

  • wktu_down (此输入包含日期时间数据,例如 09/17/2017 9:58 PM 并将使用Varchar类型数据存储到数据库)
  • wktu_up (这是相同的)
  • time_report (这将包含计算结果 wktu_up - wktu_down

我希望当我点击 time_report 表单时,它会自动用结果填充数据,我不知道我应该使用Javascript还是否。

模型

function update_model($data, $id_report){
        $this->db->where('id_report',$id_report);
        $this->db->update('report', $data);
    }

    function get_data($id_report){
       $read= $this->db->query('select * from report where id_report='.$id_report);
        if($read->num_rows() > 0){
            foreach ($read->result() as $data){
                $hasil[] = array(
                    'id_report'=>$data->id_report,
                    'wktu_down'=>$data->wktu_down,
                    'wktu_up'=>$data->wktu_up,
                    'report_time'=>$data->report_time,
                );
            }
            return $result;
        }else{
            return false;
        }
    }

1 个答案:

答案 0 :(得分:0)

对接收的数据使用datetime类型,但您可能需要重新格式化日期值,请尝试:

QSettings

要获得两个日期之间的差异,请使用DateTime::diff,如下所示:

// I get the format for your example '09/17/2017 9:58 PM'
$wktu_down = DateTime::createFromFormat('d/m/Y h:i a', $this->input->post('wktu_down'));
// The var $wktu_down is a DateTime Object and you need use the method format()
echo $wktu_down->('Y-m-d H:i:s');

可以在几分钟,几小时等内获得差异。检查DateInterval::format

不要忘记// First get the DateTime object of two dates $wktu_down = DateTime::createFromFormat('d/m/Y h:i a', $this->input->post('wktu_down')); $wktu_up = DateTime::createFromFormat('d/m/Y h:i a', $this->input->post('wktu_up')); // Get the diff $diff = $wktu_down->diff($wktu_up); // Format the diff to days echo $diff->format('%a days'); 方法的第二个参数,如果这个值包含任何XSS attack的值,您就会清理该值。

post()