Relative Time - Includes Future


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

Original script by jaytee. I used the script in a project, but then needed 'future' support. Added the script, thought I would share.


Copy this code and paste it in your HTML
  1. function relative_time($date) {
  2. $diff = time() - strtotime($date);
  3. if ($diff>0) {
  4. if ($diff<60)
  5. return $diff . " second" . plural($diff) . " ago";
  6. $diff = round($diff/60);
  7. if ($diff<60)
  8. return $diff . " minute" . plural($diff) . " ago";
  9. $diff = round($diff/60);
  10. if ($diff<24)
  11. return $diff . " hour" . plural($diff) . " ago";
  12. $diff = round($diff/24);
  13. if ($diff<7)
  14. return $diff . " day" . plural($diff) . " ago";
  15. $diff = round($diff/7);
  16. if ($diff<4)
  17. return $diff . " week" . plural($diff) . " ago";
  18. return "on " . date("F j, Y", strtotime($date));
  19. } else {
  20. if ($diff>-60)
  21. return "in " . -$diff . " second" . plural($diff);
  22. $diff = round($diff/60);
  23. if ($diff>-60)
  24. return "in " . -$diff . " minute" . plural($diff);
  25. $diff = round($diff/60);
  26. if ($diff>-24)
  27. return "in " . -$diff . " hour" . plural($diff);
  28. $diff = round($diff/24);
  29. if ($diff>-7)
  30. return "in " . -$diff . " day" . plural($diff);
  31. $diff = round($diff/7);
  32. if ($diff>-4)
  33. return "in " . -$diff . " week" . plural($diff);
  34. return "on " . date("F j, Y", strtotime($date));
  35. }
  36. }

URL: http://www.jstnjns.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.