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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:56:45  来源:igfitidea点击:

Laravel check if date is between 2 dates.

phplaraveldatetime

提问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_fromand active_untilcontains only dates it's correct, but if they contain dates with times you should probably use whereDateinstead of wherelike so:

假设两者active_fromactive_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_fromshould be >=and active_untilshould be <=

另外,查看代码我很确定您应该以另一种方式使用运算符:active_from应该>=并且active_until应该<=