简体   繁体   English

Laravel 从当前日期减去碳天数

[英]Laravel Carbon subtract days from current date

I am trying to extract objects from Model "Users" whose created_at date has been more than 30 days from today .我正在尝试从 Model "Users" 中提取对象,其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?如何做到这一点?

Use subDays() method:使用subDays()方法:

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

You can always use strtotime to 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();

From Laravel 5.6 you can use whereDate :从 Laravel 5.6 开始,您可以使用whereDate

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

You also have whereMonth / whereDay / whereYear / whereTime你还有 whereMonth / whereDay / whereYear / whereTime

As with strtotime or DateTime, any (relative) date expressions can also be used with carbon::parse .与 strtotime 或 DateTime 一样,任何(相对)日期表达式也可以与carbon::parse一起使用。

$users = Users::where('status_id', 'active')
           ->where( 'created_at', '>', Carbon::parse('Now -30 days'))
           ->get();

Alternatively for 'Now -30 Days', expressions '-30days' or '-720 hours' are also possible.或者,对于“现在 -30 天”,表达式“-30 天”或“-720 小时”也是可能的。

'Now -30 Days' takes into account the current time. “现在 -30 天”考虑了当前时间。 If you need a time 00:00, use 'Today -30 Days'.如果您需要时间 00:00,请使用“今天 -30 天”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM