Last active
August 29, 2015 13:57
-
-
Save mattnico/9519795 to your computer and use it in GitHub Desktop.
A function to display and amount of time with years, months, weeks, days, hours, minutes, and seconds.
This file contains hidden or 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 | |
/** | |
* An imprecise time formatter for imprecise, nonessential time calculations. | |
* Formats an amount of time (in seconds, minutes, hours, days, weeks, months, or years) | |
* to be displayed in years, months, weeks, days, hours, minutes, and seconds. | |
* For the purposes of this simple formatter, a month is considered 365 / 12 days. | |
* | |
* @param int $elapsed The amount of time (in seconds) to format. | |
* @param string $format The format of the outputted date string. | |
* A key letter escaped with a backslash will be replaced with | |
* the value of that time unit. The letters are: | |
* s = seconds, m = minutes, h = hours, d = days, | |
* w = weeks, l = months, y = years | |
* Uppercase letters will be padded to two digits. | |
* Examples for the value 12,345 seconds: | |
* '\H:\M:\S' will echo '03:25:45' | |
* '\hh \mm \ss' will echo '3h 25m 45s' | |
* '\yy \lmo \ww \dd \hh \mm \ss' will echo '0y 0mo 0w 0d 3h 25m 45s' | |
* @param string $units The unit of the input time: 's, m, h, d, w, l, y'. | |
* | |
* @return string The time formatted according to the $format param | |
*/ | |
function format_time($elapsed = 0, $format = "H:M:S", $units = 's'){ | |
$m_len = 60; | |
$h_len = $m_len * 60; | |
$d_len = $h_len * 24; | |
$w_len = $d_len * 7; | |
$l_len = $d_len * 365 / 12; | |
$y_len = $d_len * 365; | |
//Convert everything to seconds for simplicity | |
switch($units){ | |
case 's': | |
break; | |
case 'm': | |
$elapsed *= $m_len; | |
break; | |
case 'h': | |
$elapsed *= $h_len; | |
break; | |
case 'd': | |
$elapsed *= $d_len; | |
break; | |
case 'w': | |
$elapsed *= $w_len; | |
break; | |
case 'l': | |
$elapsed *= $l_len; | |
break; | |
case 'y': | |
$elapsed *= $y_len; | |
break; | |
} | |
//Default everything to 0 | |
$years = $months = $weeks = $days = $hours = $minutes = $seconds = 0; | |
// Check that we received a valid value in $elapsed | |
if(!is_numeric($elapsed)){ | |
return false; | |
}else{ | |
$years = floor($elapsed / $y_len); | |
$elapsed -= $years * $y_len; | |
$months = floor($elapsed / $l_len); | |
$elapsed -= $months * $l_len; | |
$weeks = floor($elapsed / $w_len); | |
$elapsed -= $weeks * $w_len; | |
$days = floor($elapsed / $d_len); | |
$elapsed -= $days * $d_len; | |
$hours = floor($elapsed / $h_len); | |
$elapsed -= $hours * $h_len; | |
$minutes = floor($elapsed / $m_len); | |
$elapsed -= $minutes * $m_len; | |
$seconds = $elapsed; | |
} | |
$time_string = preg_replace( | |
array( | |
'/(\\\\s)/', | |
'/(\\\\S)/', | |
'/(\\\\m)/', | |
'/(\\\\M)/', | |
'/(\\\\h)/', | |
'/(\\\\H)/', | |
'/(\\\\d)/', | |
'/(\\\\D)/', | |
'/(\\\\w)/', | |
'/(\\\\W)/', | |
'/(\\\\l)/', | |
'/(\\\\L)/', | |
'/(\\\\y)/', | |
'/(\\\\Y)/' | |
), | |
array( | |
$seconds, | |
str_pad($seconds, 2, "0", STR_PAD_LEFT), | |
$minutes, | |
str_pad($minutes, 2, "0", STR_PAD_LEFT), | |
$hours, | |
str_pad($hours, 2, "0", STR_PAD_LEFT), | |
$days, | |
str_pad($days, 2, "0", STR_PAD_LEFT), | |
$weeks, | |
str_pad($weeks, 2, "0", STR_PAD_LEFT), | |
$months, | |
str_pad($months, 2, "0", STR_PAD_LEFT), | |
$years, | |
str_pad($years, 2, "0", STR_PAD_LEFT), | |
), | |
$format | |
); | |
return $time_string; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment