Home > Mobile >  How can sum hours in laravel?
How can sum hours in laravel?

Time:02-23

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

$array = ['day' => 08:00, 'day' => 07:00, 'day' => 07:30] 

how to sum is 22:30

enter image description here

please help me, many thanks

CodePudding user response:

for something like...

$array = ['08:00', '07:00', '07:30']; 

Try this...

  $sum_minutes = 0;
  foreach($array as $time) {
      $explodedTime = array_map('intval', explode(':', $time ));
       $minutes = $explodedTime[0]*60 $explodedTime[1];
       $sum_minutes  = $minutes;
  }
  $hours = floor($sum_minutes/60);
  $minutes = floor($sum_minutes % 60);
  $sumTime = $hours.':'.$minutes;

btw you can't have multiple 'day' keys directly in your array like this question.

CodePudding user response:

I tried this but the minutes didn't add up

> $day_1 = Carbon::parse('08:00')->format('H:i'); 
> $day_2 = Carbon::parse('07:30')->format('H:i'); 
> $day_3 = Carbon::parse('07:00')->format('H:i');
> dd(intval($day_1) intval($day_2) intval($day_3));
=> the result is 22

its correct is 22:30

  • Related