문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top