在今天的查询构建器laravel中,datediff <15

问题描述 投票:0回答: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已经在mysql的date格式 我想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并且更喜欢它是由查询构建器或原始查询

laravel laravel-4 laravel-query-builder
2个回答
1
投票

试试这段代码

$expDate = Carbon::now()->subDays(15));
Table::whereDate('exp_date', '<',$expDate); 

0
投票

在Laravel Query中,日期差异适用于我。

DB::table('product')->whereRaw('DATEDIFF(exp_date,current_date) < 15')->get();
© www.soinside.com 2019 - 2024. All rights reserved.