我想检查今天的日期是否在数据库中存储的两个日期之间。现在我使用laravel eloquent来实现这一点:
return Set::where('type', $type)
->where('active_from', '<=', date("Y-m-d"))
->where('active_until', '>=', date("Y-m-d"))
->first();
这是否正确?
假设active_from
和active_until
都只包含日期,那么这是正确的。但是如果它们包含带有时间的日期,你应该使用whereDate
而不是where
,如下所示:
return Set::where('type', $type)
->whereDate('active_from', '<=', date("Y-m-d"))
->whereDate('active_until', '>=', date("Y-m-d"))
->first();
此外,从代码来看,我相信你应该反过来使用运算符:active_from
应该是 >=
,而 active_until
应该是 <=