Revision: 3016
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 25, 2007 02:30 by Sixer
Initial Code
Duration is a function used to turn seconds into a readable format, measured in weeks, days, hours, minutes and seconds.
Highlight: PHP
<?php
function duration($secs)
{
$vals = array('w' => (int) ($secs / 86400 / 7),
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60);
$ret = array();
$added = false;
foreach ($vals as $k => $v) {
if ($v > 0 || $added) {
$added = true;
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
?>
Sample usage
Highlight: PHP
<?php
$dateOfBirth = $someTimestamp;
$ageInSeconds = time() - $dateOfBirth;
echo 'I am ' . duration($ageInSeconds) . ' old';
?>
Initial URL
http://www.phpriot.com/d/code/date-time/duration/index.html
Initial Description
Initial Title
Translate amount of seconds to hours, minutes, seconds
Initial Tags
Initial Language
PHP