Skip to content

Instantly share code, notes, and snippets.

@nucklearproject
Created December 5, 2012 11:41
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 nucklearproject/4214927 to your computer and use it in GitHub Desktop.
Save nucklearproject/4214927 to your computer and use it in GitHub Desktop.
timeago() function in php
<?php
define("SECOND", 1);
define("MINUTE", 60 * SECOND);
define("HOUR", 60 * MINUTE);
define("DAY", 24 * HOUR);
define("MONTH", 30 * DAY);
/**
* Humanize by delta.
*
* @time the unix timestamp
* @return the human time text since time
*/
function timeago($time) {
$delta = time() - $time;
if ($delta < 1 * MINUTE)
{
return $delta == 1 ? "en este momento" : "hace " . $delta . " segundos ";
}
if ($delta < 2 * MINUTE)
{
return "hace un minuto";
}
if ($delta < 45 * MINUTE)
{
return "hace " . floor($delta / MINUTE) . " minutos";
}
if ($delta < 90 * MINUTE)
{
return "hace una hora";
}
if ($delta < 24 * HOUR)
{
return "hace " . floor($delta / HOUR) . " horas";
}
if ($delta < 48 * HOUR)
{
return "ayer";
}
if ($delta < 30 * DAY)
{
return "hace " . floor($delta / DAY) . " dias";
}
if ($delta < 12 * MONTH)
{
$months = floor($delta / DAY / 30);
return $months <= 1 ? "el mes pasado" : "hace " . $months . " meses";
}
else
{
$years = floor($delta / DAY / 365);
return $years <= 1 ? "el a&ntilde;o pasado" : "hace " . $years . " a&ntilde;os";
}
}
// Via: http://pensandoencodigo.blogspot.com.ar/2012/11/funcion-php-hace-tanto-tiempo.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment