php Laravel Carbon 从当前日期减去天数

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

Laravel Carbon subtract days from current date

phplaraveldatetimephp-carbon

提问by zeetit

I am trying to extract objects from Model "Users" whose created_atdate has been more than 30 days from today.

我正在尝试从created_at日期已经超过30 天的模型“用户”中提取对象。

Carbon::now() ==> I want as ==> Carbon::now() - 30days

Carbon::now() ==> 我想要 ==> Carbon::now() - 30 天

$users = Users::where('status_id', 'active')
               ->where( 'created_at', '<', Carbon::now())
               ->get();

How can this be achieved ?

如何做到这一点?

回答by Alexey Mezenin

Use subDays()method:

使用subDays()方法:

$users = Users::where('status_id', 'active')
           ->where( 'created_at', '>', Carbon::now()->subDays(30))
           ->get();

回答by Chris Kelker

You can always use strtotimeto minus the number of days from the current date:

您始终可以使用strtotime从当前日期减去天数:

$users = Users::where('status_id', 'active')
           ->where( 'created_at', '>', date('Y-m-d', strtotime("-30 days"))
           ->get();