Created
April 2, 2015 21:54
-
-
Save xadim/8cf3569ee14ec943c324 to your computer and use it in GitHub Desktop.
Simple snippet PHP function that turns Date interval into Human Readable Time
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
/** | |
* Format an interval to show all existing components. Human Readable Time | |
* If the interval doesn't have a time component (years, months, etc) | |
* That component won't be displayed. | |
* | |
* @param DateInterval $interval The interval | |
* | |
* @return string Formatted interval string. | |
*/ | |
function format_interval(DateInterval $interval) { | |
$result = ""; | |
if ($interval->y) { $result .= $interval->format("%y years "); } | |
if ($interval->m) { $result .= $interval->format("%m months "); } | |
if ($interval->d) { $result .= $interval->format("%d days "); } | |
if ($interval->h) { $result .= $interval->format("%h hours "); } | |
if ($interval->i) { $result .= $interval->format("%i minutes "); } | |
if ($interval->s) { $result .= $interval->format("%s seconds "); } | |
return $result; | |
} |
Very helpful, thanks!
Thanks for this, exactly what I was looking for.
Thanks for this, exactly what I was looking for.
Glad it helps-
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!