我想 `start_time` 和 `end_time` 计数如何得到这个?

如何计算start_time和end_time时间之间


$time_all = Carbon::now()->toArray();


if ($input['type'] == 'start') {

                  /*  $data = [

                        'start_time' => $time, 'contract_id' => $input['job_id'],'engineer_id' => $userid, 'type' => 'start'

                    ];*/


$start_time = JobTimingLog::create([

                        'start_time' => $time, 'contract_id' => $input['job_id'],'engineer_id' => $userid, 'type' => 'start'

                    ])->id;

$arr = array("status" => 200, "message" => "start_time Add Successfully", "data" => array('start_time' => $time, 'id' => $start_time));

} else if ($input['type'] == 'end') {

$data_end = DB::table('job_timing_logs')->where('contract_id', $input['job_id'])->where('id', $input['id'])->update(['end_time' => $time, 'type' => 'end']);

$arr = array("status" => 200, "message" => "end_time update Successfully", "data" => $times);

}

现在我想在 start_time 和 end_time 之间计算


素胚勾勒不出你
浏览 295回答 2
2回答

波斯汪

由于您没有指定从哪里获得$time变量内容以及它的格式,我假设$time是开始日期时间和$end_time结束日期时间,并且您想要获取这两者之间的时间差。可以使用Carbon 差异包来完成,如下所示:// Assume $time = '2019-12-06 00:00:00' and $end_time = '2019-12-06 18:00:00'$start_time = Carbon::parse($time); // Parse the datetime to Carbon object first$end_time = Carbon::parse($end_time);$diff_in_hours = $start_time->diffInHours($end_time);$diff_in_minutes = $start_time->diffInMinutes($end_time);$diff_in_seconds = $start_time->diffInSeconds($end_time);

慕桂英3389331

您可以使用 Carbon 的差异方法来获取两个 Carbon 对象之间的差异。例如,如果您想要 CarbonInterval 对象之间的差异$start_date,$end_date您可以执行以下操作:$start_date->diffAsCarbonInterval($end_date)
打开App,查看更多内容
随时随地看视频慕课网APP