Solution is depend on how you save login details in your database and how you handle logout process.
Note that there can be cases where user login and close the browser, do you have auto loggout when user not active for given time or some other way?
My idea is you can have insert record when user logged in to app, when user logout you can update same record with loggout time. in case of user not active for defined time duration we can consider that user as loggout.
when calculating durations you need to find users online and offline.
for online users you can calculate duration from login time to current time.
for offine users you can simply take last max login time and last max loggout time and take the duration.
Tracking users login/logout times on your site[
^]