[英]how to check current date is between two dates in laravel 5.4
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 我想将此查询转换为laravel,所以我尝试了
$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. 但是我没有输出。任何帮助将不胜感激。
Here you can use laravel whereRaw() to achieve this. 在这里,您可以使用laravel whereRaw()实现此目的。
Just like this 像这样
$dt = Carbon::now();
$getmonths= DB::table('Financial_Year')
->whereRaw('"'.$dt.'" between `start_date` and `End_date`')
->get();
You can use Carbon native function for this: 您可以为此使用Carbon本机功能:
http://carbon.nesbot.com/docs/ 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)
If you want to use only Laravel Query Builder maybe you could try: 如果您只想使用Laravel查询生成器,则可以尝试:
$date_start = Carbon::now()->startOfYear();
$date_end = Carbon::now()->endOfYear();
$query = Model::where('created_at', '<=', $date_end)
->where('created_at', '>=', $date_start)
->get();
In the variables of date_start and date_end you will put the range you want to use. 在date_start和date_end变量中,将放置您要使用的范围。 Maybe is during the same month, during the same year or you could use: 也许是在同一月份,同一年或您可以使用:
$date = Carbon::create(2012, 9, 5, 5);
to determine the date manually. 手动确定日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.