douzhenchun6782 2014-01-30 12:12
浏览 37
已采纳

如何在杂货店中减去DateTime?

The function that I need to do deals with 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. The result of such will be the value of the 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. Any help will be highly appreciated.

  • 写回答

1条回答 默认 最新

  • doukekui0914 2014-01-30 12:42
    关注

    Assuming that data in $post_array['time_ended'] and $post_array['time_started'] your function returns work_time as timestamp, not as date, so ou have to convert it to date with php function date:

    $post_array['work_time'] = date("Y-m-d H:i:s",(STRTOTIME($post_array['time_ended']) - STRTOTIME($post_array['time_started'])) )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大