next birthday date and age

7 replies to this topic

#1 brown2005

brown2005
• Members
• 943 posts

Posted 19 July 2006 - 09:27 AM

Hi, say my birthdate is 30/03/1983.

wat i want to do is use a function to say my next birthday is 30/03/2007 (cause my birthday has already gone, so it will be next year) and that i will be 24..

#2 Joe Haley

Joe Haley
• Members
• 103 posts

Posted 19 July 2006 - 09:34 AM

Simple: Check if the date has alredy passed this year, if not, say its 30/03 this year. Otherwise, 30/03 next year.

http://php.net/manua...nction.date.php

The problem comes with leap year birthdays. Simply doing it that way without checking for a leap year birthday would ocasionally tell ya someone has a birthday on a day that doesnt exist!
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

http://php.net/manual/en/index.php

#3 brown2005

brown2005
• Members
• 943 posts

Posted 19 July 2006 - 09:35 AM

thats, exactly wat I thought, but I just wondered if one of the brainy people on here that have more expierence than me had ever written a function for this...

#4 chrisprse

chrisprse
• Members
• 39 posts
• LocationUnited Kingdom

Posted 19 July 2006 - 12:18 PM

```<?php

\$birthday_day = 30;
\$birthday_month = 3;
\$birthday_year = 1983;

\$today_day = date("j");
\$today_month = date("n");
\$today_year = date("Y");
\$next_year = date("Y") + 1;

\$age = \$today_year - \$birthday_year;

if(\$today_month <= \$birthday_month) {

if(\$today_month == \$birthday_month) {

if(\$birthday_day > \$today_day) {

\$age--;

echo "Your next birthday is on ".\$birthday_day." ".\$birthday_month." ".\$today_year."";

}

else {

echo "Your next birthday is on ".\$birthday_day." ".\$birthday_month." ".\$next_year."";

}

}

else {

echo "Your next birthday is on ".\$birthday_day." ".\$birthday_month." ".\$today_year."";

}

}

else {

echo "Your next birthday is on ".\$birthday_day." ".\$birthday_month." ".\$next_year."";

}

\$age++;

echo "You will be ".\$age." on your next birthday...";

?>
```

#5 brown2005

brown2005
• Members
• 943 posts

Posted 19 July 2006 - 12:30 PM

cheers thats cool... so does that work for leap years n everything...? and how can i output the next birthday... i.e. 30th March 2007

#6 chrisprse

chrisprse
• Members
• 39 posts
• LocationUnited Kingdom

Posted 19 July 2006 - 12:34 PM

Edited to show next birthdays... It should work for leap years also, although I haven't tried. Let me know if it doesn't and I will have another look for you

#7 chrisprse

chrisprse
• Members
• 39 posts
• LocationUnited Kingdom

Posted 19 July 2006 - 12:40 PM

On a side note, if you enter the following underneath the "\$birthday_" variables:

```if((\$birthday_day == 29) && (\$birthday_month == 2)) {
\$birthday_day = 28;
}
```

This will change their birthday day to 28th Feb as you will find many people born on 29th Feb will celebrate their birthday on 28th Feb...

hth

#8 brown2005

brown2005
• Members
• 943 posts

Posted 19 July 2006 - 01:06 PM

nice one mate..