Skip to content

Instantly share code, notes, and snippets.

@nateevans
Created August 27, 2014 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nateevans/5d62f7e20954ec3d8e58 to your computer and use it in GitHub Desktop.
Save nateevans/5d62f7e20954ec3d8e58 to your computer and use it in GitHub Desktop.
<?php
/*
* Convert seconds to human readable text.
* Found at: http://csl.sublevel3.org/php-secs-to-human-text/
*
*/
function secs_to_h($secs)
{
$units = array(
"week" => 7 * 24 * 3600,
"day" => 24 * 3600,
"hour" => 3600,
"minute" => 60,
"second" => 1,
);
// specifically handle zero
if ($secs == 0) {
return "0 seconds";
}
$s = "";
foreach ($units as $name => $divisor) {
if ($quot = intval($secs / $divisor)) {
// handle fractions of seconds
if($name == 'second') $quot += $secs - floor($secs);
$s .= "$quot $name";
$s .= (abs($quot) > 1 ? "s" : "") . ", ";
$secs -= $quot * $divisor;
}
}
return substr($s, 0, -2);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment