137

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 ?

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
cosmoloc
  • 2,884
  • 5
  • 29
  • 48

4 Answers4

290

Use subDays() method:

$users = Users::where('status_id', 'active')
           ->where( 'created_at', '>', Carbon::now()->subDays(30))
           ->get();
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • 3
    Are you sure about changing '<' to '>' for getting users created more than 30 days ago? – Chris Forrence Dec 13 '16 at 13:26
  • 2
    my condition is that current date is more than 30 days from current date. I guess this logic does the task for me – cosmoloc Dec 13 '16 at 13:28
  • 3
    @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. – Chris Forrence Dec 13 '16 at 13:32
  • I guess his `more` means `later` here. Also `Carbon::now() ==> I want as ==> Carbon::now() - 30days` pretty much explains what OP wants. – Alexey Mezenin Dec 13 '16 at 13:37
  • Aah yes... sorry I guess I was not clear. Yes "later" is what i meant – cosmoloc Dec 13 '16 at 13:42
  • 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. – GeoffCodesThings Dec 18 '16 at 18:50
  • 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! – Myo Win Nov 24 '19 at 17:03
  • 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')` – tristanbailey Jan 29 '21 at 09:04
  • Are you sure it's not '<' – Damilare Koiki Dec 13 '21 at 05:41
14

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

Joskfg
  • 194
  • 1
  • 4
9

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();
Chris Kelker
  • 451
  • 4
  • 4
1

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'.

jspit
  • 7,276
  • 1
  • 9
  • 17