sawyer
sawyer

Reputation: 47

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

Upvotes: 1

Views: 1028

Answers (1)

Tipu Sultan Eiko
Tipu Sultan Eiko

Reputation: 774

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.

Upvotes: 1

Related Questions