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

uglik's avatar
Level 3

Where date range (MMDD - MMDD) overlaps

Hello, Everyone.

I have a table with seasons

Schema::create('seasons', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->date('start_date');
            $table->date('end_date');
            $table->timestamps();
        });

The table contains periods within a year. For example: low season from 09.01 to 11.30, middle season from 12.01 to 01.31, low season 02.01 to 04.31. Not tied to a specific year.

When creating a new record in the database, I need to check if the query dates overlap with existing days and months in the table, excluding the year.

    public function store(StoreSeasonRequest $request)
    {

        $start_date = Carbon::parse($request->start_date);
        $end_date = Carbon::parse($request->end_date);


        $overlap = DB::table('seasons')
            ->select(['start_date', 'end_date'])
            ->where(function ($query) use ($start_date, $end_date) {

                $query
                    ->where(function ($query) use($start_date) {
                        $query
                            ->whereMonth('start_date', '<=', $start_date->month)
                            ->whereMonth('end_date', '>=', $start_date->month);})
                    ->orWhere(function ($query) use($end_date) {
                        $query
                            ->whereMonth('start_date', '<=', $end_date->month)
                            ->whereMonth('end_date', '>=', $end_date->month);})
                    ->orWhere(function ($query) use($start_date, $end_date) {
                        $query
                            ->whereMonth('start_date', '>=', $start_date->month)
                            ->whereMonth('end_date', '<=', $end_date->month);
                    });

            })
            ->orWhere(function ($query) use ($start_date, $end_date) {

                $query
                    ->where(function ($query) use($start_date) {
                        $query
                            ->whereDay('start_date', '<=', $start_date->day)
                            ->whereDay('end_date', '>=', $start_date->day);})
                    ->orWhere(function ($query) use($end_date) {
                        $query
                            ->whereDay('start_date', '<=', $end_date->day)
                            ->whereDay('end_date', '>=', $end_date->day);})
                    ->orWhere(function ($query) use($start_date, $end_date) {
                        $query
                            ->whereDay('start_date', '>=', $start_date->day)
                            ->whereDay('end_date', '<=', $end_date->day);
                    });

            })
            ->get();

        dd($overlap);

When I try to check separately months and days, the condition works, but when I combine days or months, it doesn't work. Please tell me how can I check whether the dates include the month day - month day, excluding the year.

For example: 04.11 - 06.11 We check: 06.12 - 04.10 says it exists

Response https://i.stack.imgur.com/jJtrd.png

Maybe I should not use the date type in the table column, but an integer for example?

I need to save time periods within a calendar year and check if the days and months in the table are busy. Maybe someone has already faced a similar problem.

0 likes
3 replies
sr57's avatar

@uglik

We check: 06.12 - 04.10

don't understand ? End date is before start date ?

Please or to participate in this conversation.