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 ?
You can subtract days on current date using carbon in laravel 6, laravel 7, laravel 8 and laravel 9 version. If you need to subtract day or more days in date then you can use carbon in laravel. carbon provide subDay() and subDays() method to add days on carbon date object.
The Carbon package can be used for many purposes, such as reading the current date and time, changing the default date and time format, finding the difference between two dates, converting the date and time from one timezone to another timezone, etc.
Use subDays()
method:
$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:
$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:
$users = Users::where('status_id', 'active')
->whereDate( 'created_at', '>', now()->subDays(30))
->get();
You also have whereMonth / whereDay / whereYear / whereTime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With