I
I
Ivan2020-08-04 10:51:16
Laravel
Ivan, 2020-08-04 10:51:16

How to calculate how many users were created for the current and last week, but the week is from Sunday to Sunday?

Good afternoon. I made it to count for the current week like this:

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


And for the past:
$previous_week = User::whereDate('created_at', '<', date('Y-m-d H:i:s', strtotime('-7 days')))
            ->whereDate('created_at', '>=', date('Y-m-d H:i:s', strtotime('-14 days')))->count();


It turns out this doesn't quite fit. It is necessary that the current week is considered from today and back to Sunday, and the past from this Sunday and even before Sunday back. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-08-04
@Djonson86

Use Carbon methods in queries with created_at. An example of how to subtract weeks I wrote to you a week ago.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question