Laravel Carbon subtract days from current date
Asked Answered
Z

4

141

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 ?

Zielinski answered 13/12, 2016 at 13:25 Comment(0)
M
299

Use subDays() method:

$users = Users::where('status_id', 'active')
           ->where( 'created_at', '>', Carbon::now()->subDays(30))
           ->get();
Monogamist answered 13/12, 2016 at 13:25 Comment(9)
Are you sure about changing '<' to '>' for getting users created more than 30 days ago?Blondell
my condition is that current date is more than 30 days from current date. I guess this logic does the task for meZielinski
@AlexeyMezenin That's what the original asker is looking for; using today as an example (December 13th), "more than 30 days from today" would be users created before November 13th.Blondell
I guess his more means later here. Also Carbon::now() ==> I want as ==> Carbon::now() - 30days pretty much explains what OP wants.Monogamist
Aah yes... sorry I guess I was not clear. Yes "later" is what i meantZielinski
subDays(30) is correct but I think it should be < to get users created more than 30 days ago. Upvoted the answer anyway because of correct method.Kanpur
The answer doesn't need to be changed because subDays() subtract days. For example, today is 24-Nov-2019 and subDays(30) would return the date 24-Oct-2019. Thus if the comparison is < (less than) the result would be all records prior from 24-Oct-2019 but what OP want is later from that date and only want 30-day records. The answer is perfect!Fushih
As a note to remember you should reset H M S also to 00:00:00 or you are going to get the time difference against a Timestamp / Date Time field. so add ->endOfDay() or ->hour(0)->minute(0)->second(0) or in the date setup date('Y-m-d 00:00:00')Plastometer
Are you sure it's not '<'Calistacalisthenics
S
18

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

Subtile answered 21/1, 2021 at 17:40 Comment(1)
I like this one since it's using the now() helper.Hulett
W
10

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();
Wordbook answered 9/10, 2018 at 14:6 Comment(1)
could make it date('Y-m-d 00:00:00', strtotime("-30 days") to be more tidyPlastometer
I
3

As with strtotime or DateTime, any (relative) date expressions can also be used with 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.

'Now -30 Days' takes into account the current time. If you need a time 00:00, use 'Today -30 Days'.

Including answered 22/9, 2022 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.