Laravel之间的日期

时间:2017-11-17 20:49:53

标签: laravel eloquent

我想通过比较日期在雄辩中检查促销代码的有效性,但不知道为什么它不起作用

$promoCode = self::where('code', $code)
        ->where('start_date', '>=', Carbon::now()->toDateString())
        ->where('end_date', '<=', Carbon::now()->toDateString())
        ->first();

如果我评论第2和第3行,那么它可以正常工作。 MySQL列类型为日期。我也尝试了 whereDate 方法,而且也没有用

2 个答案:

答案 0 :(得分:1)

看起来你倒了标志。您正在寻找的时间早于start_date且小于end_date

答案 1 :(得分:1)

现在不应该是start_date。如果是这样,现在应该在。尝试:

$promoCode = self::where('code', $code)
    ->where('start_date', '<=', Carbon::now()->toDateString())
    ->where('end_date', '<=', Carbon::now()->toDateString())
    ->first();
相关问题