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.
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'])) )