代码之家  ›  专栏  ›  技术社区  ›  cosmoloc

Laravel碳从当前日期减去天数

  •  123
  • cosmoloc  · 技术社区  · 9 年前

    created_at 日期已超过 从今天起30天 .

    碳::现在()=>我想要as==>碳::现在()-30天

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

    如何实现这一点?

    3 回复  |  直到 7 年前
        1
  •  264
  •   Alexey Mezenin    9 年前

    使用 subDays() 方法:

    $users = Users::where('status_id', 'active')
               ->where( 'created_at', '>', Carbon::now()->subDays(30))
               ->get();
    
        2
  •  9
  •   Joskfg    5 年前

    从Laravel 5.6开始,您可以使用 whereDate :

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

    你也有何月/何日/何年/何时

        3
  •  8
  •   Chris Kelker    7 年前

    你可以随时使用 strtotime 要从当前日期减去天数:

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