简体   繁体   中英

Laravel check if date is between 2 dates.

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:

 return Set::where('type', $type)
            ->where('active_from', '<=', date("Y-m-d"))
            ->where('active_until', '>=', date("Y-m-d"))
            ->first();

Is this correct?

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:

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 <=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM