note 48230 added to function.time
| From: | moritz at barafranca dot com | Date: | Thu, 16 Dec 2004 05:00:55 +0000 |
| Subject: | note 48230 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81842@lists.php.net to get a copy of this message | ||
Another function that transforms a number of seconds to a more readable time.
I made it specially for displaying the duration of songs from a playlist and to display the total
time of the playlist, but it can be useful for a lot of other things.
The function comes with three types of output, one with no suffix at all, and the others being with
a short and long suffix.
<?
function duration ($seconds, $suffix=FALSE) {
$takes_time = array(604800,86400,3600,60,0);
$suffixes =
array("Week","Day","Hour","Minute","Second");
$delimeter = array(" W ", " D ", ":",":","");
$output = "";
foreach ($takes_time as $key=>$val) {
${$suffixes[$key]} = ($val == 0) ? $seconds : floor(($seconds/$val));
$seconds -= ${$suffixes[$key]} * $val;
if (${$suffixes[$key]} > 0 || (!empty($output) && $suffix == FALSE)) {
if ($val == 0 && $suffix == FALSE && empty($output)) {
$output .= "00:";
}
$output .= ($key > 1 && strlen(${$suffixes[$key]}) == 1 && $suffix == FALSE) ?
"0".${$suffixes[$key]} : ${$suffixes[$key]};
if ($suffix == "short") {
$output .= substr($suffixes[$key],0,1)." ";
}
elseif ($suffix == "long") {
$output .= (${$suffixes[$key]} > 1) ? " ".$suffixes[$key]."s " : "
".$suffixes[$key]." ";
}
else {
$output .= $delimeter[$key];
}
}
}
return $output;
}
# Examples:
echo duration(24233); // output: 06:43:53
echo duration(22303,'short'); // output: 6H 11M 43S
echo duration(22309,'long'); // output: 6 Hours 11 Minutes 49 Seconds
?>
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+48230
Delete -- http://master.php.net/manage/user-notes.php?action=delete+48230&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48230&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48230&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48230&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48230&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48230&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+48230&report=yes
Search -- http://master.php.net/manage/user-notes.php