Created
September 13, 2013 12:27
-
-
Save micc83/6550016 to your computer and use it in GitHub Desktop.
Relative date by @Pellegrom
This file contains 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 relative_time($date, $postfix = ' ago', $fallback = 'F Y') | |
{ | |
$diff = time() - strtotime($date); | |
if($diff < 60) | |
return $diff . ' second'. ($diff != 1 ? 's' : '') . $postfix; | |
$diff = round($diff/60); | |
if($diff < 60) | |
return $diff . ' minute'. ($diff != 1 ? 's' : '') . $postfix; | |
$diff = round($diff/60); | |
if($diff < 24) | |
return $diff . ' hour'. ($diff != 1 ? 's' : '') . $postfix; | |
$diff = round($diff/24); | |
if($diff < 7) | |
return $diff . ' day'. ($diff != 1 ? 's' : '') . $postfix; | |
$diff = round($diff/7); | |
if($diff < 4) | |
return $diff . ' week'. ($diff != 1 ? 's' : '') . $postfix; | |
$diff = round($diff/4); | |
if($diff < 12) | |
return $diff . ' month'. ($diff != 1 ? 's' : '') . $postfix; | |
return date($fallback, strtotime($date)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment