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';
?>
snippets |
December 15, 2011
Translate amount of seconds to hours, minutes, seconds
Leave a Reply Cancel reply
You must be logged in to post a comment.