<?php
/**
* Time Duration Functions
* Created by: Joshua H. (TRUSTAbyss)
*
* Usage: get_secs_duration(day, hr, min, sec) will return the number
* of seconds based on the time duration given; fmt_secs_duration(secs)
* will return day(s) hour(s) min(s) sec(s) in an array.
*/
function get_secs_duration($d = 0, $h = 0, $m = 0, $s = 0)
{
// Multiply each number to its correct value in seconds.
$d *= 86400; $h *= 3600; $m *= 60; $s += 0;
// Return the total number of seconds for day(s), hr(s), min(s), sec(s).
return $d + $h + $m + $s;
}
function fmt_secs_duration($seconds)
{
// Set the values of day, hour, min, sec.
$d =
floor($seconds/
86400);
$h =
floor(($seconds%
86400)/
3600);
$m =
floor(($seconds%
3600)/
60);
$s = $seconds%60;
// Return an associative array with the values of d = day, h = hour, m = min, s = sec.
return array('d' =>
$d,
'h' =>
$h,
'm' =>
$m,
's' =>
$s);
}
// Example on using both functions together. Try leaving out some of the arguments in get_secs_duration()
// function and see what the output is like. Here's the arguments in order: day, hour, min, sec.
$seconds = get_secs_duration(0, 2, 30, 15); // returns 9015 sec(s).
$dur = fmt_secs_duration($seconds);
// We finally output the results of the array from fmt_secs_duration() function.
echo "Time Duration: {$dur['d']} day(s), {$dur['h']} hour(s), {$dur['m']} min(s), {$dur['s']} sec(s)";
?>