2

我的表格产品中有这样的字段

--exp_date--
2016-08-02
2016-08-28
2016-08-28
2016-08-23
2016-08-15
2016-08-05
2016-08-20

exp_date已经在datemysql 中格式化了
我想要select数据从今天起剩余的日期exp_date少于 15

我已经尝试过这样的事情

$dn = date('Y-m-d');          //today date
$query = DB::table('product')->where(DB::raw('datediff(exp_date), $dn') < 15)->get();

但我得到这个错误
Object of class Illuminate\Database\Query\Expression could not be converted to int

如何解决?

ps:
我使用 laravel 4.2 并且更喜欢它是通过查询构建器或原始查询

4

2 回答 2

6

日期差异在 Laravel Query 中对我有用。

DB::table('product')->whereRaw('DATEDIFF(exp_date,current_date) < 15')->get();
于 2018-02-09T14:05:49.387 回答
5

试试这个代码

$expDate = Carbon::now()->subDays(15));
Table::whereDate('exp_date', '<',$expDate); 
于 2016-08-20T18:29:39.583 回答