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

lara68236's avatar

Finding records that have a date range, using a single date

I have a database with two fields that are dates. A start field. A end field.

If the date I am using to search is 2019-8-10, what would the query look like in Laravel to find all records whose start and end are either on, or between this date?

Thanks in advance for your help.

0 likes
5 replies
bobbybouwmann's avatar

In general the query is the other way around right? You have a start and end date in your input that you use check if a date is available in the given period.

Anyway, in general date queries are really easy in Laravel to do. In your case that might look like this

$date = Carbon::now();

$models = Model::where('start_date',  '>=', $date)
    ->where('end_date', '<=', $date)
    ->get();

This will get you all records that are either on that date or between that date.

Snapey's avatar

how can something be 'between' a single date?

do you mean between today and that date?

lara68236's avatar

I started with pretty much exactly as you suggesed, but I did not get the results I should have. Later I found that I needed to do the reverse with the where clause, so this is what I did:

$data['player_plays'] = PlayerPlay::where([
  ['play_date_start', '<=', $draw_date],
  ['play_date_end', '>=', $draw_date],
  ['game_id', '=', $game_id]
  ])->with('plays')
  ->get();

Once I did that everything came together.

Thanks for your input.

lara68236's avatar

I may not have worded that correctly.

The record has to dates, start/end, and the query has only one. It needs to match one of those, or be between them.

jlrdw's avatar

The following has always worked fine for me, where the variables are the dates in proper format.

...
where (`transactions`.`TransactionDate` Between :bdate and  :edate) ";
...

Total query not shown but proper binding takes place.

If the date I am using to search is 2019-8-10

Also use the correct date format in a query of the parent database, if MySql it would be:

2019-08-10

// Not

2019-8-10

Please or to participate in this conversation.