Reputation: 1285
I have a table
+---------------------+-----------
| id | start_date | End_date |
+---------------------+-----------+
| 1 | 2017-07-07 | 2017-07-31|
| 2 | 2017-08-01 | 2017-08-31|
| 3 | 2017-09-01 | 2017-09-10|
|
+------+--------------+-----------+
And I want to select dates between two dates.I have a query
SELECT * FROM Financial_Year WHERE CURDATE() between `start_date` and `End_date`
I want to convert this query to laravel so i tried this
$dt = Carbon::now();
$getmonths= DB::table('Financial_Year')
->whereBetween($dt, ['start_date', 'End_date'])->get();
But i didn't get output.Any help would be appreciated.
Upvotes: 7
Views: 13649
Reputation: 487
If you want to use only Laravel Query Builder maybe you could try:
$date = Carbon::now();
$query = Model::where('start_date', '<=', $date)
->where('end_date', '>=', $date)
->get();
Upvotes: 2
Reputation: 1840
If you want to check a single user input date with two database columns
->where('start_date', '<=', $date)
->where('end_date', '>=', $date)->exists();
Upvotes: -1
Reputation: 15057
You can use Carbon native function for this:
http://carbon.nesbot.com/docs/
$first = Carbon::create(2012, 9, 5, 1);
$second = Carbon::create(2012, 9, 5, 5);
var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true)
Upvotes: 2
Reputation: 4904
Here you can use laravel whereRaw() to achieve this.
Just like this
$dt = Carbon::now();
$getmonths= DB::table('Financial_Year')
->whereRaw('"'.$dt.'" between `start_date` and `End_date`')
->get();
Upvotes: 14