Advertisement
LucianoCharles2017

Soma Horas

May 25th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1. <?php
  2.  
  3. function somarHoras(array $times) {
  4.  
  5.     $seconds = 0;
  6.  
  7.     foreach ($times as $time) {
  8.         list( $g, $i, $s ) = explode(':', $time);
  9.         $seconds += $g * 3600;
  10.         $seconds += $i * 60;
  11.         $seconds += $s;
  12.     }
  13.  
  14.     $hours = floor($seconds / 3600);
  15.     $seconds -= $hours * 3600;
  16.     $minutes = floor($seconds / 60);
  17.     $seconds -= $minutes * 60;
  18.  
  19.     $seconds = ($seconds == 0 || $seconds <= 9) ? "0{$seconds}" : $seconds;
  20.     $minutes = ($minutes == 0 || $minutes <= 9) ? "0{$minutes}" : $minutes;
  21.     $hours = ($hours <= 9) ? "0{$hours}" : $hours;
  22.  
  23.     return "{$hours}:{$minutes}:{$seconds}";
  24. }
  25.  
  26. $times = [
  27.     '01:31:22',
  28.     '03:29:38',
  29. ];
  30. echo somarHoras($times);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement