Skip to content

Instantly share code, notes, and snippets.

@deyvin
Created June 4, 2012 12:46
Show Gist options
  • Save deyvin/2868130 to your computer and use it in GitHub Desktop.
Save deyvin/2868130 to your computer and use it in GitHub Desktop.
relative time php
<?php
function plural($num) {
if ($num != 1)
return "s";
}
function getRelativeTime($date) {
$diff = time() - strtotime($date);
if ($diff<60)
return $diff . " segundo" . plural($diff) . " atrás";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minuto" . plural($diff) . " atrás";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hora" . plural($diff) . " atrás";
$diff = round($diff/24);
if ($diff<7)
return $diff . " dia" . plural($diff) . " atrás";
$diff = round($diff/7);
if ($diff<4)
return $diff . " semana" . plural($diff) . " atrás";
return "on " . date("F j, Y", strtotime($date));
}
echo getRelativeTime("2012-06-01 00:00:00");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment