The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
Time Duration Functions
Posted by: TRUSTAbyss | December 5, 2007 @ 8:54am
Revision 4 - Original post on December 4, 2007 @ 1:20pm
PHP Code
[
Download
]
<?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)"; ?>
Syntax Highlighting
[
Open in new window
]
Author Comments
The "Time Duration Functions" allow you to get the number of seconds from a given time duration, or format the time duration from a given number of seconds. An example is provided.
Rating
4.54 / 8
365 Votes