【问题标题】:How to subtract DateTime in grocery crud?如何在杂货杂货中减去 DateTime?
【发布时间】:2014-02-22 17:45:45
【问题描述】:

我需要处理 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());
    }
}

在回调的帮助下,我要做的是从 time_end 中减去 time_started。这样的结果将是 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;
}

问题从这里开始,它返回 0000-00-00 00:00:00 并将该值存储到数据库中。任何帮助将不胜感激。

【问题讨论】:

  • "它返回 0000-00-00 00:00:00" - 你的意思是该函数返回 0 还是将 0000-00-00 00:00:00 保存到数据库?
  • @MarkS 它将 0000-00-00 00:00:00 保存到数据库中。

标签: php mysql datetime time grocery-crud


【解决方案1】:

假设 $post_array['time_ended']$post_array['time_started'] 中的数据您的函数将 work_time 作为时间戳返回,而不是作为日期,因此您必须使用 php 函数 date 将其转换为日期:

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

【讨论】:

  • 我觉得我需要评论我的答案,因为它不能完全满足您的需求:您的问题是您想从 work_time 存储“时间量”,但数据库中的日期字段可以存储“特定日期/时间”,所以基本上你应该将你的字段更改为整数,并简单地存储完成工作所需的秒数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多