0

enter image description here

There are 3 fields in task table-> expected_start_datetime,expected_end_datetime,time_allocated While creating a task expected start and end datetime is selected and saved in the records.

What I am trying to do is to find the difference between the two dates in hours and minutes and save the value inside the "time_allocated" while creating the task and later on the update or view page use/display the time allocated value from the records.

Trying something like this in the task controller action create

$diff = ((strtotime($model->expected_start_datetime) - strtotime($model->expected_end_datetime)) / (60 * 60 * 24));

        $model->time_allocated = $model->time_allocated + $diff;

enter image description here

ArK
  • 20,698
  • 67
  • 109
  • 136
deepak
  • 62
  • 2
  • 8

1 Answers1

1

in your model you should be override beforeSave function like this:

            public function beforeSave($insert) {

                $diff =strtotime($this->expected_end_datetime)-strtotime($this->expected_start_datetime);
                $hours= floor($diff/(60*60));
                $mins= floor(($diff-($hours*60*60))/60);

                $this->time_allocated=$hours.':'.sprintf("%02d",$mins);
                return parent::beforeSave($insert);
            }
Jalali
  • 596
  • 4
  • 19