You can start by save the login time tick in a variable, just like this:
OnPcLoginEvent:
set tick, gettimetick(2);
When the player logs out, you can subtract the setted tick with an actual tick, then sum it with the value on DB, just like this:
OnPcLogoutEvent:
set time, gettimetick(2) - tick;
This will give you the time (in seconds) that the player were online. I'll let the queries part up to you!