If you’re storing the sessions in the db then you could have a field in the session table which would be a timestamp (set when the user logs in) and then when either the user logs out or their session expires, the elapsed time between when they logged on and when they logged off would be calculated and added to the relevant field in the user table
Just because a session expires after ‘x’ hours doesn’t mean that the person was actually on the site all that time. They may have only been there a few seconds and then forgot to log off leaving their session to expire based on settings on the server.
If they have javaScript enabled then you could set up a script on the page that calls the server at regular intervals to advise that they are still on the site. You would then know that they had left when more than that amount of time passed without the script making another call.