I
I
Ivan2020-07-29 18:01:15
Laravel
Ivan, 2020-07-29 18:01:15

How to select users created in the previous week in laravel?

For the current week I choose

$count = Users::whereDate('created_at', '>=', date('Y-m-d H:i:s', strtotime('-7 days')))->count();


For the previous one (stay between 7 days and 14 days) I try to choose something like this:
$count = Users::whereDate([
            ['created_at', '<', date('Y-m-d H:i:s', strtotime('-7 days'))],
            ['created_at', '>=', date('Y-m-d H:i:s', strtotime('-14 days'))],
        ])->count();

But errors are thrown. Perhaps the syntax is wrong, or does not support "and", I really could not google for this whereDate function. How to do it? Or maybe there is a ready-made function for this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2020-07-29
@Djonson86

$count = Users::whereDate('created_at', '<', date('Ymd H:i:s', strtotime('-7 days')))->whereDate('created_at', '>=', date('Ymd H:i:s', strtotime('-14 days')))->count();

J
jazzus, 2020-07-29
@jazzus

The previous week is not between minus 14 and 7 days. For weeks:

User::whereBetween('created_at', [
        now()->startOfWeek()->subWeek(), 
        now()->startOfWeek()
      ])
    ->count();

days
User::whereBetween('created_at', [
    now()->subDays(14), 
    now()->subDays(7)
  ])
->count();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question