Laravel 检查日期是否在 2 个日期之间。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47212695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel check if date is between 2 dates.
提问by Jenssen
I want to check if today's date is between to dates dat are stored in the database. Right now I do this with laravel eloquent:
我想检查今天的日期是否介于 dat 存储在数据库中的日期之间。现在我用 laravel eloquent 做到这一点:
return Set::where('type', $type)
->where('active_from', '<=', date("Y-m-d"))
->where('active_until', '>=', date("Y-m-d"))
->first();
Is this correct?
这样对吗?
回答by Marcin Nabia?ek
Assuming both active_from
and active_until
contains only dates it's correct, but if they contain dates with times you should probably use whereDate
instead of where
like so:
假设两者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();
Also, looking at the code I'm pretty sure you should use operators the other way: active_from
should be >=
and active_until
should be <=
另外,查看代码我很确定您应该以另一种方式使用运算符:active_from
应该>=
并且active_until
应该<=