Skip to content

Instantly share code, notes, and snippets.

@shevabam
Last active October 21, 2015 08:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shevabam/53079974a2e9e3bf504a to your computer and use it in GitHub Desktop.
Save shevabam/53079974a2e9e3bf504a to your computer and use it in GitHub Desktop.
SecondsToHumanReadableText
<?php
/**
* Seconds to human readable text
* Eg: for 36545627 seconds => 1 year, 57 days, 23 hours and 33 minutes
*
* @return string Text
*/
function getHumanTime($seconds)
{
$units = array(
'year' => 365*86400,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
// 'second' => 1,
);
$parts = array();
foreach ($units as $name => $divisor)
{
$div = floor($seconds / $divisor);
if ($div == 0)
continue;
else
if ($div == 1)
$parts[] = $div.' '.$name;
else
$parts[] = $div.' '.$name.'s';
$seconds %= $divisor;
}
$last = array_pop($parts);
if (empty($parts))
return $last;
else
return join(', ', $parts).' and '.$last;
}
?>
@PifyZ
Copy link

PifyZ commented Jul 16, 2015

Add .php at the file name to coloring ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment