Jump to content

[SOLVED] Days until birthday


jaymc

Recommended Posts

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

Link to comment
Share on other sites

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!";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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");

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.