1. <?php
  2. /**
  3.  * Time Duration Functions
  4.  * Created by: Joshua H. (TRUSTAbyss)
  5.  *
  6.  * Usage: get_secs_duration(day, hr, min, sec) will return the number
  7.  * of seconds based on the time duration given; fmt_secs_duration(secs)
  8.  * will return day(s) hour(s) min(s) sec(s) in an array.
  9.  */
  10.  
  11. function get_secs_duration($d = 0, $h = 0, $m = 0, $s = 0)
  12. {
  13. // Multiply each number to its correct value in seconds.
  14. $d *= 86400; $h *= 3600; $m *= 60; $s += 0;
  15.  
  16. // Return the total number of seconds for day(s), hr(s), min(s), sec(s).
  17. return $d + $h + $m + $s;
  18. }
  19.  
  20. function fmt_secs_duration($seconds)
  21. {
  22. // Set the values of day, hour, min, sec.
  23. $d = floor($seconds/86400);
  24. $h = floor(($seconds%86400)/3600);
  25. $m = floor(($seconds%3600)/60);
  26. $s = $seconds%60;
  27.  
  28. // Return an associative array with the values of d = day, h = hour, m = min, s = sec.
  29. return array('d' => $d, 'h' => $h, 'm' => $m, 's' => $s);
  30. }
  31.  
  32. // Example on using both functions together. Try leaving out some of the arguments in get_secs_duration()
  33. // function and see what the output is like. Here's the arguments in order: day, hour, min, sec.
  34.  
  35. $seconds = get_secs_duration(0, 2, 30, 15); // returns 9015 sec(s).
  36. $dur = fmt_secs_duration($seconds);
  37.  
  38. // We finally output the results of the array from fmt_secs_duration() function.
  39. echo "Time Duration: {$dur['d']} day(s), {$dur['h']} hour(s), {$dur['m']} min(s), {$dur['s']} sec(s)";
  40. ?>