Jump to content

How can i get next year by birthday ?


kb5220

Recommended Posts

Hi developer.

 

I am writing here because I am shock into a problem.

 

I am starting to make a page that runs much of dates.

 

Example.

I sign on page and then I will write a date from the birthday. let and say I would write my birthday as 08-02-1991 then it could figure out when I once again have a birthday / one week before.

 

Thanks in advance for your help. :D

Link to comment
Share on other sites

You can use strtotime. An example script

// set the date of birth
$dob = '08-02-1991';

// get the timestamp for the dob
$dobTime = strtotime($dob);

// get the timestamp 1 week before the dob timestamp
$dob1WeekBefore = strtotime('-1 Week', $dobTime);

echo 'Birthday:<br />' . date('D jS M Y', $dobTime) . '<br /><br />';

echo 'Week Before Birthday:<br />' . date('D jS M Y', $dob1WeekBefore);

 

Note. strtotime will not work with any dates before 1st of Jan 1970.

Link to comment
Share on other sites

You'll need to calculate the difference in years (current year - dob year)

// get the dob year
list(,,$dobyear) = explode('-', $dob);
// get the difference in years
// add 1 to get the date for next years birthday
$years = (date('Y') - $dobyear) + 1;

 

date('Y') returns the current year.

 

We'll use this code to get the date a week before the birthday next year

$dob1WeekBefore = strtotime("+$years year -1 Week", $dobTime);

 

Full code

$dob = '08-02-1991';

$dobTime = strtotime($dob);

list(,,$dobyear) = explode('-', $dob);
$years = (date('Y') - $dobyear) + 1;
$dob1WeekBefore = strtotime("+$years year -1 Week", $dobTime);


echo 'You where born on:<br />' . date('D jS M Y', $dobTime) . '<br /><br />';

echo 'Week before your birthday:<br />' . date('D jS M Y', $dob1WeekBefore);

Link to comment
Share on other sites

Thanks it was really helpful. :)

 

Now i made this

<?php
$day = "12";
$month = "06";
$year = "1991";

if (date("d") <= $day && date("m") <= $month){
echo date("d-m-Y", mktime(0,0,0,$month,$day - 7,date("Y")));
}else {
$dob = "$day-$month-$year";

$dobTime = strtotime($dob);

list(,,$dobyear) = explode('-', $dob);
$years = (date('Y') - $dobyear) + 1;
$dob1WeekBefore = strtotime("+$years year -1 Week", $dobTime);

echo date('d-m-Y', $dob1WeekBefore);
}
?>

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.