从今天开始,查询构建器 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
where datediff < 15 from today in query builder laravel
提问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_date
already in date
format in mysql
I want to select
data which date remaining exp_date
less than 15 from today
exp_date
已经在date
mysql中的格式
我想要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);