问题
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 ?
回答1:
Use subDays() method:
$users = Users::where('status_id', 'active')
->where( 'created_at', '>', Carbon::now()->subDays(30))
->get();
回答2:
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();
来源:https://stackoverflow.com/questions/41122265/laravel-carbon-subtract-days-from-current-date