Jump to content

Archived

This topic is now archived and is closed to further replies.

brown2005

next birthday date and age

Recommended Posts

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..

any help please?

Share this post


Link to post
Share on other sites
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/manual/en/function.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!

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
[code=php:0]
<?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...";

?>
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
On a side note, if you enter the following underneath the "$birthday_" variables:

[code=php:0]
if(($birthday_day == 29) && ($birthday_month == 2)) {
$birthday_day = 28;
}
[/code]

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.