date.php 817 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. class Date
  3. {
  4. public static function ago ($datetime)
  5. {
  6. $estimate_time = time() - $datetime;
  7. if( $estimate_time < 1 )
  8. return 'right now';
  9. $condition = array(
  10. 12 * 30 * 24 * 60 * 60 => 'year',
  11. 30 * 24 * 60 * 60 => 'month',
  12. 24 * 60 * 60 => 'day',
  13. 60 * 60 => 'hour',
  14. 60 => 'minute',
  15. 1 => 'second');
  16. foreach ($condition as $secs => $str)
  17. {
  18. $d = $estimate_time / $secs;
  19. if ($d >= 1)
  20. {
  21. $r = round ($d);
  22. return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
  23. }
  24. }
  25. }
  26. }