Search code examples
phplaravelphp-carbonlaravel-9

How can sum hours in laravel?


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


Solution

  • 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 ));
          $sum_minutes += $explodedTime[0]*60+$explodedTime[1];
      }
      $sumTime = floor($sum_minutes/60).':'.floor($sum_minutes % 60);
    

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