Yii2 datetime 计算两个日期之间的差异并将差异插入 db
Yii2 datetime difference calculation between two dates and inserting the difference in db
任务中有3个字段table-> expected_start_datetime,expected_end_datetime,time_allocated
在创建任务时,预期的开始和结束日期时间被选择并保存在记录中。
我想要做的是找到两个日期之间的小时和分钟差异,并在创建任务时将值保存在 "time_allocated" 中,稍后在更新或查看页面上保存 use/display 记录中的时间分配值。
在任务控制器操作创建中尝试类似的东西
$diff = ((strtotime($model->expected_start_datetime) - strtotime($model->expected_end_datetime)) / (60 * 60 * 24));
$model->time_allocated = $model->time_allocated + $diff;
在您的模型中,您应该像这样覆盖 beforeSave 函数:
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);
}
任务中有3个字段table-> expected_start_datetime,expected_end_datetime,time_allocated 在创建任务时,预期的开始和结束日期时间被选择并保存在记录中。
我想要做的是找到两个日期之间的小时和分钟差异,并在创建任务时将值保存在 "time_allocated" 中,稍后在更新或查看页面上保存 use/display 记录中的时间分配值。
在任务控制器操作创建中尝试类似的东西
$diff = ((strtotime($model->expected_start_datetime) - strtotime($model->expected_end_datetime)) / (60 * 60 * 24));
$model->time_allocated = $model->time_allocated + $diff;
在您的模型中,您应该像这样覆盖 beforeSave 函数:
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);
}