Posted By

jcroom on 08/21/09


Tagged

php date count days birthday


Versions (?)

Who likes this?

16 people have marked this snippet as a favorite

jcroom
vali29
NyX
rickfu
maxvee8
umang_nine
silentbob84
farondomenicgmailcom
nb109
lolindirfaelivrin
shawjopc
archangel
tewoos
voove
telemichus
ringo380


Count days until date


 / Published in: PHP
 

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.

  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  

You need to login to post a comment.