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

jerauf's avatar

Finding whether current date is between 2 dates in mysql

I'm building a parking app and I want to show the list of cars that are registered for parking between 2 dates.

This is my query:

$date = Carbon::now(); $now = $date->toDateString();

$parking = Parking::where('start_date', '>=', $now) ->where('end_date', '<=', $now) ->orderBy('start_date') ->get();

start_date and end_date are date fields.

However, nothing is returned even though I have sample data with parking that has a start date that's a couple of days before today and end date that's a couple of days after today.

Is there something wrong with my query?

0 likes
8 replies
martinbean's avatar

@jerauf I don't think your query is what you intended. What you’re asking for is, “find all records where start date is greater than now, and end date is before now.” That’s never going to evaluate to true because you’re never going to have a record where the start date is after the end date.

As an aside, you don’t ned to use ->toDateString(). Laravel will automatically handle the conversion of Carbon instances when used to query date and date time columns.

jerauf's avatar

@martinbean I have sample data that has a start date of 7/1 and an end date of 7/10. Why wouldn't my query return that?

martinbean's avatar

@jerauf Because of what I’ve already explained:

What you’re asking for is, “find all records where start date is greater than now, and end date is before now.” That’s never going to evaluate to true because you’re never going to have a record where the start date is after the end date.

That condition is never going to evaluate as true.

If you want to find “current” bookings (i.e. a booking that started in the past, but ends in the future) then you need to swap the conditions around:

$now = Carbon::now();

$parking = Parking::query()
    ->where('start_date', '<', $now)
    ->where('end_date', '>', $now)
    ->get();

It might also be nicer to wrap those conditions into a local scope:

class Parking extends Model
{
    public function scopeActive(Builder $query): Builder
    {
        $now = Carbon::now();

        return $query
            ->where('start_date', '<', $now)
            ->where('end_date', '>', $now);
    }
}
$parking = Parking::query()->active()->get();
jerauf's avatar

@martinbean But without <= and >=, if someone books parking for today (which is the common use case), it's only evaluating start date in the past. Or am I mistaken?

Snapey's avatar

@jerauf you can add = if you want the date to be inclusive of today

jlrdw's avatar

@jerauf can you please explain how the current date can be between two dates. I would forget about the now part and just use your necessary dates. From and to..

jerauf's avatar

@jlrdw If the current date is 7/2 and the start date of the parking is 7/1, end date is 7/10, $now would be between those 2 dates.

martinbean's avatar

If the current date is 7/2 and the start date of the parking is 7/1, end date is 7/10, $now would be between those 2 dates.

@jerauf Exactly! So:

Start date = 7/1. Today = 7/2. End date = 7/10

Start date (7/1) is less than (<) 7/2; not greater than or equal to (>=) like you were trying to do. Your logic was the wrong way around.

Please or to participate in this conversation.