Question

Every user log in ,I save log in date by:

if(Auth::check()){
      $user = Auth::user();
      $user->last_login_at = date('Y-m-d H:i:s');
      $user->save();
}

Now, how can I get number of users that logged in last month?

Was it helpful?

Solution

As I see you have some Auth stuff, you surely have the User model somewhere in your app. You can fetch the users you want using the below code (untested but should work for you):

//users logged during a period of month ago from now
User::where('last_login_at', '>=', new DateTime('-1 months'))->get(); 

//users logged from the beggining of current callendar month
User::where('last_login_at', '>', new DateTime('last day of previous month'))->get();


EDIT:

The "-1 months" or "last day of previous month" are the so called DateTime Relative Formats - check the below documentation for futher details and interesting ideas on date calculations.

http://www.php.net/manual/en/datetime.formats.relative.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top