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

chrisgrim's avatar

Searching If Date Range Is within Date Range issue

Hi, I am tyring to figure out what I am doing wrong with my code. I have

        $thursday = Carbon::now()->startOfWeek()->addDays(3); 
        $wednesday = Carbon::now()->startOfWeek()->addDays(9); 
        $staffpick = StaffPick::where(function($query) use ($thursday, $wednesday){
                $query->whereDate('start_date', '<=', $thursday)
                       ->whereDate('end_date', '>=', $wednesday);
            })
            ->orderBy('rank')
            ->first();
return $staffpick;

but this returns nothing even though I know I have a model with the dates start_date = 2020-08-27 00:00:00 and end_date = 2020-09-02 00:00:00

0 likes
7 replies
bobbybouwmann's avatar
Level 88

The problem is the comparison. Right now you only want to select the items that are NOT in that date range, but you also use two different date fields. I'm not sure if that is correct.

This translated now to: start date is earlier than 2020-08-27 and end date is later than 2020-09-02.

chrisgrim's avatar

Hi @bobbybouwmann

I am doing a staff picks where I let the staff pick events to feature from the start_date to the end_date. So I want to check to see if the date range they selected overlaps with Thur of this week -> Wed of next week. I tried doing

return $staffpick = StaffPick::where(function($query) use ($thursday, $wednesday){
                $query->whereDate('start_date', '>=', $thursday)
                       ->whereDate('end_date', '<=', $wednesday);
            })
            ->orderBy('rank')
            ->first();

and it still returns nothing.

MichalOravec's avatar

Problem is that you compare date with datetime.

So just change them to the date with ->toDateString()

$query->whereDate('start_date', '<=', $thursday->toDateString())
    ->whereDate('end_date', '>=', $wednesday->toDateString());
chrisgrim's avatar

Hi @michaloravec So I tried

$thursday = Carbon::now()->startOfWeek()->addDays(3)->toDateString(); 
        $wednesday = Carbon::now()->startOfWeek()->addDays(9)->toDateString(); 
        $staffpick = StaffPick::where(function($query) use ($thursday, $wednesday){
                $query->whereDate('start_date', '<=', $thursday)
                        ->whereDate('end_date', '>=', $wednesday);
            })
            ->orderBy('rank')
            ->first();
        return $staffpick;

and it returns nothing. If I do

$staffpick = StaffPick::orderBy('rank')
            ->first();
        return $staffpick;

it returns

id": 1,
"event_id": 215,
"user_id": 1,
"rank": 5,
"start_date": "2020-08-25 00:00:00",
"end_date": "2020-08-26 00:00:00",
"created_at": "2020-08-25T19:55:39.000000Z",
"updated_at": "2020-08-25T20:11:17.000000Z",
"comments": "xsxzsxzsxzsxzsx",

So at least the model is there.

MichalOravec's avatar

Ok but that data in your database are not in your range.

Please or to participate in this conversation.