Laravel Carbon subtract days from current date

zeetit picture zeetit · Dec 13, 2016 · Viewed 140k times · Source

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

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

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

How can this be achieved ?

Answer

Alexey Mezenin picture Alexey Mezenin · Dec 13, 2016

Use subDays() method:

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