How to save session end time

Hello,
I want to have records of users login and logout time

How can I save end of session time if a user ends activity by closing the browser ?

There’s no 100% reliable way to do this.

One option is to store the time of the user’s last request in a database, then use this as a rough guide of when the user has left your site - for example, you may judge that a user that has made no requests for five minutes has left.