Facebok Style Timeago (advanced)


/ Published in: PHP
Save to your folder(s)

i dont see who is the author of this script, but i want to say him a lot thanks and would like to performance to him (and to everybody) a modification of this script.. i have modified it a bit and it is absolutely like on fb.. new "featchures" are:

instead -
* 1-59 seconds, minutes, hours - nothing modified;
* 1 day ago: Yesterday at --:--
* 2-7 days ago: Monday (or other day of the week) at --:--
* 1 week ago etc.: Dec 27, 2013 at 22:06 (for example)


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function ago($time) {
  4. $diff = time() - (int)$time;
  5.  
  6. if ($diff == 0) {
  7. return 'Just now';
  8. }
  9.  
  10. $intervals = array(
  11. 1 => array('year', 31556926),
  12. $diff < 31556926 => array('month', 2628000),
  13. $diff < 2629744 => array('week', 604800),
  14. $diff < 604800 => array('day', 86400),
  15. $diff < 86400 => array('hour', 3600),
  16. $diff < 3600 => array('minute', 60),
  17. $diff < 60 => array('second', 1)
  18. );
  19.  
  20. $value = floor($diff/$intervals[1][1]);
  21. $ago = $value.' '.$intervals[1][0].($value > 1 ? 's' : '');
  22.  
  23. $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  24.  
  25. $day = $days[date('w', $time)];
  26.  
  27. if ($ago == '1 day') {
  28. return 'Yesterday at '.date('H:i', $time);
  29. }
  30. elseif ($ago == '2 days' || $ago == '3 days' || $ago == '4 days' || $ago == '5 days' || $ago == '6 days' || $ago == '7 days') {
  31. return $day.' at '.date('H:i', $time);
  32. }
  33. elseif ($value <= 59 && $intervals[1][0] == 'second' || $intervals[1][0] == 'minute' || $intervals[1][0] == 'hour') {
  34. return $ago.' ago';
  35. }
  36. else {
  37. return date('M', $time).' '.date('d', $time).', '.date('Y', $time).' at '.date('H:i', $time);
  38. }
  39. }
  40.  
  41. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.