Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RonB1985's avatar

Carbon between only day and month

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
    }
0 likes
12 replies
d3xt3r's avatar

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?

???

RonB1985's avatar

Apologies, I can be confusing ...

What I want to check is, if today falls between two certain dates. So, example:

$today = 12-01-2016
$date1 = 10-01-2016
$date2 = 15-01-2016

I want to check if $today falls between $date1 and $date2. But I only want to check if the day and month match. I can use this to check:

    if ($today->between($date1, $date2))
    {
        echo 'Match';
    }

But that would include the years. If I have this:

$today = 12-01-2017
$date1 = 10-01-2016
$date2 = 15-01-2016

Then the following won't match:

    if ($today->between($date1, $date2))
    {
        echo 'Match';
    }

And I cannot do this:

$today->format('m-d');
$date1->format('m-d');
$date2->format('m-d');

Because the Carbon 'between' method would return an error since it cannot check on a string. Once I use the ->format on a date, it's not a Carbon instance anymore.

Hope that helped, if not, I will just drop this for now since it's frying my brains right now .. :)

jimmck's avatar

You can't compare dates without years.

d3xt3r's avatar

@RonB1985 I am not sure why you need this, but if this is what you require then

public function isBetween($date, $start, $end) {

        $format = 'd-m-Y';

        $d = (int) Carbon::createFromFormat($format,$date)->format('md');
        $s = (int) Carbon::createFromFormat($format,$start)->format('md');
        $e = (int) Carbon::createFromFormat($format,$end)->format('md');

        return $s <= $d && $d <= $e;
    }

$today = '12-01-2017';
$date1 = '10-01-2016';
$date2 = '15-01-2016';

$this->isBetween($today,$date1,$date2);
    
1 like
kfirba's avatar

@RonB1985 you can simply achieve that by changing the dates a bit. Just force the year:

$date1->year = $today->year;
$date2->year = $today->year;

// now just make the comparison..
$today->isBetween($date1, $date2);
1 like
RonB1985's avatar

Awesome, that works like a charm indeed, thanks a lot, still lots to learn.. :)

RonB1985's avatar

Edit: Unfortunately both methods did not work for when I had to check for let's say between 25-12 and 03-01, no matter which years I was using. I will have to keep looking further for a solution but at least I am on the right track with it now, thanks to you both :)

d3xt3r's avatar

honestly, i am having trouble understanding your requirement and use case now, you said years don't matter and then you are switching it.

If not considering the year 27-12 will never lie between 25-12 and 3-01.

RonB1985's avatar

True, but it was still a work in progress and now I think I finally got it. Your method did work but then I tested with the between december and january and that didn't work. So I had to go with another method. Now, I set all my date's years to the same year before I start adding months and subtracting days. And that does seem to do the trick.

I was trying to explain what I am trying to achieve, but it is way too confusing to explain, I also just tried to explain it to my girlfriend and it was not easy trying to explain it. So I won't bother you with it. In short, I want to be able to check if a date which I grab from the database lies between a range of two other dates. And this I wanted to do every 3 months, so I had to add months, subtract days, and so on. Too hard to explain hehe, but again, thanks a lot for the help. Your answer, d3xt3r, still is the right answer for my original question. kfirba's answer however was the one that finally got it working for me.

Thanks again, you've helped me from a few more nights of headache :)

d3xt3r's avatar

Ya, you bet, but am having one right now :)

1 like
RonB1985's avatar

In the end I gave up on it, there was always one date that wouldn't work, because of the years. But I had to include years, otherwise the 'december -> january' thing wouldn't work, for example. Oh well, no more headaches tonight, maybe some other time when something pops in my head again.. :P

Please or to participate in this conversation.