Mehran
Mehran

Reputation: 195

Retrieve data between dates in laravel

I am using an eloquent query to retrieve data from a table. The table columns look like this:

id started_at finished_at
1 06/02/2019 06/15/2019
2 06/05/2019 06/11/2019

What I want to do is, given a $date (ex: 06/08/2019 ) and get the data of the row, that the $date between started at and finished_at columns.

Upvotes: 0

Views: 56

Answers (3)

Aditya Thakur
Aditya Thakur

Reputation: 2610

Try this

$data = Model::where('started_at', '<', $date)->where('finished_at', '>', $date)->first();

Upvotes: 0

Karan
Karan

Reputation: 1156

$date = date('Y-m-d',strtotime($started_at));


$data = Model::where('started_at', '>', $date)->where('finished_at', '<', $date)->first();

Upvotes: 0

Vishal Thakkar
Vishal Thakkar

Reputation: 61

 DB::table('table_name')->whereBetween('started_at', [$date1, $date2])
  ->orWhereBetween('finished_at', [$date1, $date2])->get();

Upvotes: 1

Related Questions