jaymc Posted January 10, 2009 Share Posted January 10, 2009 Can anyone give me an example of how to output the number of days until a birthday e.g, my birthday is 3rd January 1987 I thought it would be easy e.g birthdate timestamp minus current timestamp but the problem is Id have to pretend the birthday was 3rd January 2009 It gets tricky if the current date is 29th December 2008 for instance Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 10, 2009 Share Posted January 10, 2009 You also need to factor in leap years. What format is the input date in? Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 10, 2009 Author Share Posted January 10, 2009 You also need to factor in leap years. What format is the input date in? Yeh, thats what I meant by tricky ha! Its in unix timestamp, but I can convert to anything providing it gets the job done Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 10, 2009 Share Posted January 10, 2009 here you can do it like this $birthday = "3rd January"; $days = round(abs(strtotime($birthday)-time()) / 86400); echo $days; 86400 is 60*60*24 or the number of seconds in one day Scott. Quote Link to comment Share on other sites More sharing options...
dclamp Posted January 10, 2009 Share Posted January 10, 2009 try using strtotime() $birthday = "20th January 2009"; $bts = strtotime($birthday); $ts = time(); if ($bts > $ts) { // checks if birthday is AFTER todays date $remaining = $bts - $ts; echo "Seconds: " . $remaining; // Will output in seconds echo "Days: " . ($remaining / 86400); // Outputs days remaining } else { echo "Birthday Already Passed!"; } Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 11, 2009 Author Share Posted January 11, 2009 Neither of your solutions will work as lets say someone has a birthday of 2 Jan 1987 I would use 2 Jan 2009 and I'd do that by checking current year and adding it onto day "2 Jan $year" But what if it was december 30th 2009, there next birthday would show as "2 Jan $year" which would be "2 Jan 2009" hence its in the past when it needed to be "2 Jan 2010" Thats the problem, its not as simple as date - date = second remaining Quote Link to comment Share on other sites More sharing options...
dclamp Posted January 11, 2009 Share Posted January 11, 2009 Neither of your solutions will work as lets say someone has a birthday of 2 Jan 1987 I would use 2 Jan 2009 and I'd do that by checking current year and adding it onto day "2 Jan $year" But what if it was december 30th 2009, there next birthday would show as "2 Jan $year" which would be "2 Jan 2009" hence its in the past when it needed to be "2 Jan 2010" Thats the problem, its not as simple as date - date = second remaining Thats not our problem. Our scripts bother give the seconds remaining given the birthday is AFTER todays date. My script Will tell you if it is past or not. Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 11, 2009 Share Posted January 11, 2009 $birthday = "20th January" $bts = strtotime($birthday + " " + date("y")); $ts = time(); if ($bts < $ts) { $bts = strtotime($birthday + " " + date("y",strtotime("+1 year"))); //go to next year for dates in the past } $remaining = $bts - $ts; echo "Days: " . round($remaining / 86400); // Outputs days remaining Scott. Quote Link to comment Share on other sites More sharing options...
jaymc Posted January 11, 2009 Author Share Posted January 11, 2009 Few little glitches in that but yeh thats what I was looking for. This works perfect function days_till_birthday($birth_day_month) { $bts = strtotime($birth_day_month." ".date("y")); $ts = time(); if ($bts < $ts) {$bts = strtotime($birth_day_month." ".date("y",strtotime("+1 year")));} return round(($bts - $ts) / 86400); } echo days_till_birthday("20 Jan"); Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 11, 2009 Share Posted January 11, 2009 sorry lol been doing lots of coding in java today and got mixed up lol Scott. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.