Skip to content

Instantly share code, notes, and snippets.

@bshaffer
Created May 25, 2010 15:02
Show Gist options
  • Save bshaffer/413227 to your computer and use it in GitHub Desktop.
Save bshaffer/413227 to your computer and use it in GitHub Desktop.
get the duration of a timestamp
<?php
function duration($seconds, $max_periods = 3)
{
$periods = array("year" => 31536000, "month" => 2419200, "week" => 604800, "day" => 86400, "hour" => 3600, "minute" => 60, "second" => 1);
$i = 1;
foreach ( $periods as $period => $period_seconds )
{
$period_duration = floor($seconds / $period_seconds);
$seconds = $seconds % $period_seconds;
if ( $period_duration == 0 )
{
continue;
}
$duration[] = "{$period_duration} {$period}" . ($period_duration > 1 ? 's' : '');
$i++;
if ( $i > $max_periods )
{
break;
}
}
return implode(' ', $duration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment