Read it thrice, but couldn't make any sense of you are trying to do or need.
how can I check if a date is between two other dates, but only by month and day?
???
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Please bear with me and my stupid code below. I am just trying to get this to work first, then work on the coding.
My main question is, how can I check if a date is between two other dates, but only by month and day? Either with Carbon or any other way. Explanation below:
So I am trying to ech something on the screen if it is a match. I have a start date which can be anything. From that date I create 3 more dates, each with 3 months apart from each other. Then, I check if today is between a start and end date which I create from the given date. Check out my code below.
The thing is, this works if I use the $date = '2015-10-25' because when adding months, $start4 and $end4 will end up being this year's date. The thing with Carbon's between method is that it requires a Carbon instance.
if(date1->between(someDate,someOtherDate))
I ony want to check the days and months. So for example:
if(month-day->between(month-day,month-day)
But if I use format('m-d'), I get an error, cannot format on string, which is obvious. So my main question would be, how can I check if a date is between two other dates, but only by month and day? Either with Carbon or any other way.
$date = '2016-10-25';
$start1 = \Carbon\Carbon::parse($date)->subDays('5'); // Gives 2016-10-20
$start2 = \Carbon\Carbon::parse($date)->addMonths('3')->subDays('5'); // Gives 2016-01-20
$start3 = \Carbon\Carbon::parse($date)->addMonths('6')->subDays('5'); // Gives 2016-04-20
$start4 = \Carbon\Carbon::parse($date)->addMonths('9')->subDays('5'); // Gives 2016-07-20
$end1 = \Carbon\Carbon::parse($date); // Gives 2016-10-25
$end2 = \Carbon\Carbon::parse($date)->addMonths('3'); // Gives 2016-01-25
$end3 = \Carbon\Carbon::parse($date)->addMonths('6'); // Gives 2016-04-25
$end4 = \Carbon\Carbon::parse($date)->addMonths('9'); // Gives 2016-07-25
$today = \Carbon\Carbon::now(); // Gives 2016-07-23
if ($today->between($start1, $end1))
{
echo 'START1';
}
if ($today->between($start2, $end2))
{
echo 'START2';
}
if ($today->between($start3, $end3))
{
echo 'START3';
}
if ($today->between($start4, $end4)) // Gives if(2016-07-23 is between 2017-07-20 and 2017-07-25)
{
echo 'START4'; // Should be a hit but because it also checks on the years, it is not
}
Please or to participate in this conversation.