Created
March 7, 2012 17:55
-
-
Save Medalink/1994697 to your computer and use it in GitHub Desktop.
Date Helper
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 | |
class Date { | |
public static function ago( $time ) { | |
$time = time() - 25; | |
$periods = array( 'second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade' ); | |
$lengths = array('60','60','24','7','4.35','12','10'); | |
$now = time(); | |
$difference = $now - $time; | |
$tense = 'ago'; | |
for( $j = 0; $difference >= $lengths[$j] && $j < count( $lengths ) - 1; $j++ ) { | |
$difference /= $lengths[$j]; | |
} | |
$difference = round( $difference ); | |
if( $difference != 1 ) { | |
$periods[$j] .= 's'; | |
} | |
$return = $difference . ' ' . $periods[$j] . ' ago'; | |
if( $return == '0 seconds ago' ){ | |
return 'Just Now'; | |
} | |
return $return; | |
} | |
} |
cviebrock
commented
Mar 7, 2012
array('%d second ago', '%d seconds ago'),
'minutes' => array('%d minute ago', '%d minutes ago'),
// ...
);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment