Created
April 30, 2015 01:13
-
-
Save raisiqueira/9cc1367f133e6df3bf3f to your computer and use it in GitHub Desktop.
Função "time ago"
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 | |
//http://codeforgeek.com/2014/10/time-ago-implementation-php/ | |
function get_timeago( $ptime ) | |
{ | |
$estimate_time = time() - $ptime; | |
if( $estimate_time < 1 ) | |
{ | |
return 'less than 1 second ago'; | |
} | |
$condition = array( | |
12 * 30 * 24 * 60 * 60 => 'year', | |
30 * 24 * 60 * 60 => 'month', | |
24 * 60 * 60 => 'day', | |
60 * 60 => 'hour', | |
60 => 'minute', | |
1 => 'second' | |
); | |
foreach( $condition as $secs => $str ) | |
{ | |
$d = $estimate_time / $secs; | |
if( $d >= 1 ) | |
{ | |
$r = round( $d ); | |
return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago'; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment