Created
June 13, 2010 05:55
-
-
Save geoloqi/436389 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Convert minutes into a human-readable format. | |
* i.e. 60 minutes = 1 hour, 10080 minutes = 1 week | |
* | |
* Examples: | |
* 1530 minutes is 1 day, 1 hour, 30 minutes | |
* minutesToText(1530, array('rounded'=>0)) = 1 day | |
* minutesToText(1530, array('rounded'=>1)) = 1.1 days | |
* minutesToText(1530, array('units'=>1)) = 1 day | |
* minutesToText(1530, array('units'=>2)) = 1 day, 1 hour | |
* minutesToText(1530, array('units'=>3)) = 1 day, 1 hour, 30 minutes | |
* | |
* @param int $minutes | |
* @param int [0] $round If > 0, only one unit is returned, rounded to this many places | |
* @param int [1] $numUnits The number of different units to return | |
* @param boolean [FALSE] $abbr If TRUE, uses 'min' instead of 'minutes | |
* @return string | |
*/ | |
static public function minutesToText($input, $opts=array()) | |
{ | |
$round = 0; | |
$numUnits = 1; | |
$abbr = FALSE; | |
extract($opts); | |
if($abbr) | |
$units = array( | |
525600 => 'yr', | |
40320 => 'mth', | |
10080 => 'wk', | |
1440 => 'day', | |
60 => 'hr', | |
1 => 'min', | |
); | |
else | |
$units = array( | |
525600 => 'year', | |
40320 => 'month', | |
10080 => 'week', | |
1440 => 'day', | |
60 => 'hour', | |
1 => 'minute', | |
); | |
$diff = $input; | |
foreach($units as $m=>$label) | |
{ | |
$$label = floor($diff / $m); | |
if($round > 0 && $$label > 0) | |
return ($r = round($input / $m, $round)) > 1 | |
? $r . ' ' . $label . 's' | |
: $r . ' ' . $label; | |
$diff -= $$label * $m; | |
} | |
$labels = array_values($units); | |
$r = array(); | |
for($i = 0; $i < count($units) && count($r) < $numUnits; $i++) | |
if(${$labels[$i]} > 0) | |
$r[] = ${$labels[$i]} . ' ' . $labels[$i] . (${$labels[$i]} > 1 ? 's' : ''); | |
return implode(', ', $r); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment