Home > Mobile >  How to plus or minus hours in laravel?
How to plus or minus hours in laravel?

Time:02-23

I have the following case but don't know how to do it, can anyone help me.

Clock Clock In: 8:00, Clock Out: 17:00 => Total: 9:00

1st break Start Break: 11:00, End Break: 13:00 => Total break1: 2:00

Second break Start Break: 15:00, End Break: 15:30 => Total break2: 0:30

$startTime = Carbon::parse('8:00'); 
$endTime = Carbon::parse('17:00');
$duration = $endTime->diffInMinutes($startTime);
$totalminutes  = $duration;
dd(date('H:i', mktime(0, $totalminutes)));
Total : 9:00

I want to plus the hours of break1 and break2

2:00 0:30 = 2:30

and total clock minus total break

9:00 - 2:30 = 6:30

everyone helps me, thanks everyone

CodePudding user response:

You can calculate the duration the same way for breaks except that you need to subtract them from the total minutes instead of adding them.

$startTime = Carbon::parse('8:00');
$endTime = Carbon::parse('17:00');

$firstBreakStart = Carbon::parse('11:00');
$firstBreakEnd = Carbon::parse('13:00');

$secondBreakStart = Carbon::parse('15:00');
$secondBreakEnd = Carbon::parse('15:30');

$totalMinutes = $startTime->diffInMinutes($endTime);
$totalMinutes -= $firstBreakStart->diffInMinutes($firstBreakEnd);
$totalMinutes -= $secondBreakStart->diffInMinutes($secondBreakEnd);

dd(date('H:i', mktime(0, $totalMinutes)));
  • Related