Skip to content

Instantly share code, notes, and snippets.

@feload
Last active August 29, 2015 14:09
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 feload/eb3c2960b4be9134c0ba to your computer and use it in GitHub Desktop.
Save feload/eb3c2960b4be9134c0ba to your computer and use it in GitHub Desktop.
Human readable elapsed time.
<?php
function time_elapsed_string($ptime){
$etime = time() - $ptime;
if ($etime < 1)
return '0 seconds';
$a = array( 12 * 30 * 24 * 60 * 60 => 'año',
30 * 24 * 60 * 60 => 'meses',
24 * 60 * 60 => 'día',
60 * 60 => 'hora',
60 => 'minuto',
1 => 'segundo'
);
foreach ($a as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
if($r > 1)
{
$end = ($str == 'mes') ? 'es' : 's';
}
return "Hace {$r} {$str}{$end}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment