time between now and future date. (days, hours, minutes, seconds


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

modified version of:
http://www.linein.org/blog/2008/04/04/find-time-between-two-dates-in-php/


Copy this code and paste it in your HTML
  1. function timeBetweenNowAndDeadline($deadline) {
  2. $currentDate = date("U");
  3. $diff = $deadline-$currentDate;
  4. $seconds = 0;
  5. $hours = 0;
  6. $minutes = 0;
  7.  
  8. if($diff % 86400 <= 0){$days = $diff / 86400;} // 86,400 seconds in a day
  9. if($diff % 86400 > 0)
  10. {
  11. $rest = ($diff % 86400);
  12. $days = ($diff - $rest) / 86400;
  13. if($rest % 3600 > 0)
  14. {
  15. $rest1 = ($rest % 3600);
  16. $hours = ($rest - $rest1) / 3600;
  17. if($rest1 % 60 > 0)
  18. {
  19. $rest2 = ($rest1 % 60);
  20. $minutes = ($rest1 - $rest2) / 60;
  21. $seconds = $rest2;
  22. }
  23. else{$minutes = $rest1 / 60;}
  24. }
  25. else{$hours = $rest / 3600;}
  26. }
  27.  
  28. if($days > 0){$days = $days.'d, ';}
  29. else{$days = false;}
  30. if($hours > 0){$hours = $hours.'h, ';}
  31. else{$hours = false;}
  32. if($minutes > 0){$minutes = $minutes.'m, ';}
  33. else{$minutes = false;}
  34. $seconds = $seconds.'s'; // always be at least one second
  35.  
  36. return $days.''.$hours.''.$minutes.''.$seconds;
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.