Count days until date


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

I made this script to count the days until a birthday, the birthdays are stored in mysql with the year intact YYYY-MM-DD, so this function strips the first 4 chars, and adds the current year, if the date has already passed it adds the next year and counts for that.


Copy this code and paste it in your HTML
  1. function countdays($d)
  2. {
  3. $olddate = substr($d, 4);
  4. $newdate = date(Y) ."".$olddate;
  5. $nextyear = date(Y)+1 ."".$olddate;
  6.  
  7. if($newdate > date("Y-m-d"))
  8. {
  9. $start_ts = strtotime($newdate);
  10. $end_ts = strtotime(date("Y-m-d"));
  11. $diff = $end_ts - $start_ts;
  12. $n = round($diff / 86400);
  13. $return = substr($n, 1);
  14. return $return;
  15. }
  16. else
  17. {
  18. $start_ts = strtotime($nextyear);
  19. $end_ts = strtotime(date("Y-m-d"));
  20. $diff = $end_ts - $start_ts;
  21. $n = round($diff / 86400);
  22. $return = substr($n, 1);
  23. return $return;
  24.  
  25. }
  26.  
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.