How to capture login time when logging out in Laravel?

I am building an application with Laravel 5, and I must have a module to save and update the user's login hours.

I am new to programming, so I'm going crazy about the logic of how to do this.

This is for call center agents. Their shift is between. 3pm to 6amSo, for example, they go in with a timestamp 2015-07-21 03:00:00, which 3pm, and then if they log out 4pm, they have 1.0hours of logging in, then they go in 7pmand go out again 5am of 2015-07-21, then this will be their previous hour of logging in, which 1.0+ 10.0because of 7pm 2015-07-20 - 5am 2015-07-21. And should update the login hours of this user.

How can i do this? So now I have a table in my database that

logihours
 ->id
 ->user_id
 ->loginhours (double/float)
 ->timestamp (timestamp of last login/logout)
 ->status (if currently logged in or logged out

That is, now, of course, I need to always check my functions loginand logout, but I don’t know where to start.

+4
source share
2 answers

If I decided to solve this problem, I would save the timestamp in the database under the name last_loginthat was stored the last time the user successfully logged in.

Then when you log out, you can compare the current timestamp with the timestamp stored in the database to determine how long they have been logged in.

Warning: Another potential problem is that the same user can have multiple sessions (maybe they go to their desktop and to their phone?).

+1
source

entrance_logs
 ->id
 ->user_id
 ->loged_in_at [default=null]
 ->loged_out_at [default=null]

login_hours
 ->id
 ->user_id
 ->hours [default=0]

. login_logs loged_in_at, , user_id loged_out_at. - login_hours Laravel cron.

:

:

$EntranceLog = new EntranceLog();
$EntranceLog->user_id = Auth::user()->id;
$EntranceLog->loged_in_at = date('Y-m-d H:i:s');
$EntranceLog->save();

:

$EntranceLog = EntranceLog::where('loged_in_at', '>=', date('Y-m-d H:i:s', time()-86400))
                          ->whereNull('loged_out_at')
                          ->where('user_id', '=', Auth::user()->id)
                          ->orderBy('loged_in_at', 'desc')->first();
if($EntranceLog) {
  $EntranceLog->loged_out_at = date('Y-m-d H:i:s');
  $EntranceLog->save();

  $hours = strtotime($EntranceLog->loged_out_at) - strtotime($EntranceLog->loged_in_at);
  $hours/= 3600;
  $hours = (double)$hours;

  $LoginHour = LoginHour::where('user_id', '=', Auth::user()->id)->first();
  if(!$LoginHour) {
    $LoginHour = new LoginHour();
    $LoginHour->user_id = Auth::user()->id;
  }
  $LoginHour->hours = (double)$LoginHour->hours + $hours;
  $LoginHour->save();
}

:

+1

Source: https://habr.com/ru/post/1598856/


All Articles