Skip to content

Instantly share code, notes, and snippets.

@xadim
Created April 2, 2015 21:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xadim/8cf3569ee14ec943c324 to your computer and use it in GitHub Desktop.
Save xadim/8cf3569ee14ec943c324 to your computer and use it in GitHub Desktop.
Simple snippet PHP function that turns Date interval into Human Readable Time
/**
* 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;
}
@johnsnook
Copy link

thanks!

@doughertym
Copy link

Very helpful, thanks!

@RyanNutt
Copy link

Thanks for this, exactly what I was looking for.

@xadim
Copy link
Author

xadim commented Nov 25, 2021

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