简体   繁体   English

如何在杂货店中减去DateTime?

[英]How to subtract DateTime in grocery crud?

The function that I need to do deals with DateTime. 我需要做的功能是处理DateTime。

public function dtr() {
    try {
        $crud = new grocery_CRUD();

        $crud->set_theme('flexigrid');
        $crud->set_table('dtr');
        $crud->set_subject('Employee DTR');
        $crud->required_fields('employee_id', 'time_started', 'time_ended');
        $crud->display_as('employee_id','Name')
             ->display_as('date','Date')
             ->display_as('time_started','Time Started')
             ->display_as('time_ended','Time Ended');

         $crud->set_relation('employee_id', 'employee', '{employee_fname} {employee_mname} {employee_lname}');

        $crud->columns('employee_id', 'time_started', 'time_ended');
        $crud->fields('employee_id', 'time_started', 'time_ended', 'work_time');
        $crud->field_type('work_time', 'hidden');
        $crud->field_type('normal_time', 'hidden');
        $crud->field_type('over_time', 'hidden');

        $crud->callback_before_update(array($this,'Working_time'));
        $crud->callback_before_insert(array($this,'working_time'));

        $output = $crud->render();

        $this->output($output);
        } catch(Exception $e) {
            show_error($e->getMessage().' --- '.$e->getTraceAsString());
    }
}

With the help of callback, what I'm going to do is subtract time_started from time_ended. 在回调的帮助下,我要做的是从time_ended中减去time_started。 The result of such will be the value of the work_time. 这样的结果将是work_time的值。

public function working_time($post_array, $primary_key = null) {
    $post_array['work_time'] = (STRTOTIME($post_array['time_ended']) - STRTOTIME($post_array['time_started'])) / 3600;
    return $post_array;
}

The problem starts here, it returns 0000-00-00 00:00:00 and stores that value into the database. 问题从这里开始,它返回00:00:00并将该值存储到数据库中。 Any help will be highly appreciated. 任何帮助将受到高度赞赏。

假设$post_array['time_ended']$post_array['time_started']你的函数返回work_time作为时间戳,而不是日期,所以你必须用php函数日期将它转换为日期:

$post_array['work_time'] = date("Y-m-d H:i:s",(STRTOTIME($post_array['time_ended']) - STRTOTIME($post_array['time_started'])) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM