Time Elapsed Function


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

Use by sending a unix timestamp to countTime(timestamp). It will return something like 40 seconds , 2 weeks, 1 hour. Then just format to your needs like we do in our app: Someone was here 10 minutes ago.


Copy this code and paste it in your HTML
  1. function countTime($given) {
  2. $current=time();
  3. $passed=$current-$given;
  4. if($given=='')
  5. {
  6. $passed=100000000000;
  7. }
  8. $months=$passed/2628000;
  9. $weeks=$passed/604800;
  10. $days=$passed/86400;
  11. $hours=$passed/3600;
  12. $minutes=$passed/60;
  13. if($passed>(2628000*12))
  14. {
  15. $r='Long time';
  16. }
  17. elseif($months>=1)
  18. {
  19. $rem=$months-round($months,0);
  20. $months=round($months,0);
  21. $r=$months." month";
  22. if($months>1)
  23. {
  24. $r=$r.'s';
  25. }
  26. }
  27. elseif($weeks>=1)
  28. {
  29. $weeks=round($weeks,0);
  30. $r=$weeks." week";
  31. if($weeks>1)
  32. {
  33. $r=$r.'s';
  34. }
  35. }
  36. elseif($days>=1)
  37. {
  38. $days=round($days,0);
  39. $r=$days." day";
  40. if($days>1)
  41. {
  42. $r=$r.'s';
  43. }
  44. }
  45. elseif($hours>=1)
  46. {
  47. $hours=round($hours,0);
  48. $r=$hours." hour";
  49. if($hours>1)
  50. {
  51. $r=$r.'s';
  52. }
  53. }
  54. elseif($minutes>=1)
  55. {
  56. $minutes=round($minutes,0);
  57. $r=$minutes." minute";
  58. if($minutes>1)
  59. {
  60. $r=$r.'s';
  61. }
  62. }
  63. else
  64. {
  65. $r=$passed." second";
  66. if($passed>1)
  67. {
  68. $r=$r.'s';
  69. }
  70. }
  71. return $r;
  72. }

URL: http://www.gruburg.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.