Advertisement
badlogic

sum of hours HHMMSS in a loop and convert to seconds

Nov 23rd, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1.  
  2.  
  3. pag sum lahat ng oras HH:MM:SS
  4.  
  5. at pag convert ng HH:MM:SS to seconds
  6.  
  7.  
  8. <?php
  9.  $times = array();
  10.  
  11. $times[] = "01:00:00";
  12. $times[] = "01:30:60";
  13.  
  14. // pass the array to the function
  15. echo AddPlayTime($times);
  16.  
  17. function AddPlayTime($times) {
  18.     $minutes = 0; //declare minutes either it gives Notice: Undefined variable
  19.     $seconds = 0;
  20.     // loop throught all the times
  21.     foreach ($times as $time) {
  22.         list($hour, $minute, $seconds1) = explode(':', $time);
  23.    
  24.         $minutes += $hour * 60;
  25.         $minutes += $minute;
  26.         $seconds += $seconds1;
  27.        // echo $seconds.'   ';
  28.     }
  29.    
  30.  
  31.  
  32.     $hours = floor($minutes / 60);
  33.     $minutes -= $hours * 60;
  34.  
  35.  
  36.     // returns the time already formatted
  37.     return sprintf('%02d:%02d:%02d', $hours, $minutes,$seconds);
  38. }
  39.  
  40.  
  41. $str_time = AddPlayTime($times);
  42.  
  43. $str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
  44.  
  45. sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
  46.  
  47. $time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
  48. echo "</br>";
  49. echo $time_seconds;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement