从今天开始,查询构建器 laravel 中的 datediff < 15

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39057339/
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 14:20:55  来源:igfitidea点击:

where datediff < 15 from today in query builder laravel

laravellaravel-4laravel-query-builder

提问by Neversaysblack

I have field in my table product like this

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

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

exp_datealready in dateformat in mysql
I want to selectdata which date remaining exp_dateless than 15 from today

exp_date已经在datemysql中的格式
我想要select数据exp_date从今天起不到 15 的日期

i aleady try something like this

我先试试这样的

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

but i get this error
Object of class Illuminate\Database\Query\Expression could not be converted to int

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

how to fix it?

如何解决?

ps:
i used laravel 4.2 and prefer it's by query builder or raw query

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

回答by Kavi Joshi

Date Difference works for me in Laravel Query.

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

DB::table('product')->whereRaw('DATEDIFF(exp_date,current_date) < 15')->get();

回答by Parithiban

Try This code

试试这个代码

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