添加时间类型列

时间:2019-09-12 06:04:25

标签: php laravel for-loop time

我不知道如何增加时间

WrokController

if($request->status === 'completed')
    {
        $t = 0;
        $alltime = Work::where('project_id', $project->id)->get();
        foreach($alltime as $time)
        {
            $t = $t + $time->work_time;
        }
        $project -> fill(['total_work_time' => $t])->save();
    }

但是没有很好的方法吗?

1 个答案:

答案 0 :(得分:2)

您可以用时间间隔对其求和,

$base_time = new DateTime();
$time_now = clone $base_time;
foreach ($alltime as $time) {
    $array = explode(':', $time);
    $base_time->add(new DateInterval(sprintf('PT%dH%dM', $array[0], $array[1])));
}
$sum_of_diff = $time_now->diff($base_time);