Created
June 4, 2012 12:46
-
-
Save deyvin/2868130 to your computer and use it in GitHub Desktop.
relative time php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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