note 48230 deleted from function.time by aidan

From: Date: Sat, 08 Oct 2005 01:04:16 +0000
Subject: note 48230 deleted from function.time by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96618@lists.php.net to get a copy of this message
Note Submitter: moritz at barafranca dot com ---- 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 ?>

« previous php.notes (#96618) next »