Skip to content

Instantly share code, notes, and snippets.

@Medalink
Created March 7, 2012 17:55
Show Gist options
  • Save Medalink/1994697 to your computer and use it in GitHub Desktop.
Save Medalink/1994697 to your computer and use it in GitHub Desktop.
Date Helper
<?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
Copy link

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