Get upper and lower date of ranges for birthday timeframe in PHP

86 views Asked by At

I am trying to calculate two dates, the date a week before and the date eight weeks after a user´s birthday.

In our example we would like to grant the user access to some specials in a timeframe around his birthday. 1 week before his birthday until 8 weeks after his birthday

e.g. #1

User birthday: 1988-01-05
Timeframe should be: YYYY-12-29 to YYYY-03-03

Current Date:  
1) 2016-12-30 
2) 2017-01-01 
3) 2017-02-01
4) 2016-12-28
5) 2017-12-28

Result should be:
1) True
2) True
3) True
4) False
5) False

e.g. #2
User birthday: 1988-12-30
Timeframe should be: YYYY-12-23 to YYYY-02-24

Current Date: 
1) 2016-12-30 
2) 2017-01-01 
3) 2017-02-01
4) 2016-12-28
5) 2017-12-28

Result should be:
1) True
2) True
3) True
4) True
5) True

Has anyone tried to accomplish something similar and can help me out here with some basic example, to light the bulb over my head?

1

There are 1 answers

6
Jordan Soltman On

This can be easily achieved with the carbon library.

$dt = Carbon::create(1991, 1, 31, 0); // Set the birthday
$dt->addWeeks(8); // Gives you a date 8 weeks after
$dt->subWeek(); // Gives you a date 1 week before

And then you don't have to deal with crazy date manipulation which is a huge pain.