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

vishytk's avatar

get data if a date is between start_date and end_date

I have following code

$date = Carbon::today()->format('d-m-Y');

Want to retrieve data if user_id matches and the $date is between start_date and end_date

Leave::where('user_id', $user->id)
            ->where(function ($query) use ($date) {
                $query->where('start_date', '<=', $date)
                    ->where('end_date', '>=', $date);
            })->get();

The above query does not return any data.

Note: the value of $date => '09-02-2022', start_date => '2022-02-08 00:00:00' and end_date => '2022-02-09 10:30:00'

0 likes
4 replies
NemoPS's avatar

Hmm why are you using the closure? You should be able to chain where clauses.

edit: are they in that format? I believe the format should match what's in the database

vishytk's avatar

I have tried the following, but getting same result

Leave::where('user_id', $user->id)
                ->where('start_date', '<=', $date)
                ->where('end_date', '>=', $date)
            ->get();
Sofia's avatar
Sofia
Best Answer
Level 6

Use 'Y-m-d' to format your date. If it's still not working, inspect the query that's being generated and post here.

vishytk's avatar

@Sofia changing the format $date format to 'Y-m-d' worked.

Thanks

Please or to participate in this conversation.