Jump to content

[SOLVED] check how long user has been logged in


Hyaku_

Recommended Posts

Hi!
I'm trying to check, for how long user has been logged in. This is basic idea, but it doesn't quite work right:
[code]echo "You have been logged in for " . round((date("ymdHis") - $_SESSION['LAST_ACTIVE'])/60) . " minutes.";
$_SESSION['LAST_ACTIVE'] = date("ymdHis");[/code]
But sometimes I get alot more minutes, than he's really logged in. I'm not sure if it's a good idea, to include even year, month, date, but for example if the time is 23:45 and then it goes to 00:15, it will screw up! Any suggestions how to make this work? Thank you!
A lowercase g shows the hour (12 hour format) and a lowercase i shows the minutes(with leading zeros)

[code]
echo "You have been logged in for " . round((date("gi") - $_SESSION['LAST_ACTIVE'])/60) . " minutes.";
$_SESSION['LAST_ACTIVE'] = date("gi");\
[/code]
For this use unix timestamps for your math with time, and then convert the timestamp at the time you display. This way, it won't matter if you are going from 23:45 to 00:15, the seconds in between are going to be the same no matter what.

I would do all the calculations before I do the echo so that the echo is as simple as possible.
[code]
$timeloggedin= (time()  - $_SESSION['LAST_ACTIVE]) / 60 
/* 
would take now[time()] - lastactive time then divide the results by 60
*/

echo "You have been logged in for $timeloggedin";
[/code]
I found a function script on google that will convert the seconds difference into a time like 1:15:23  for 1 hour 15 minutes 23 seconds. If you cannot locate it lemme know and I will give it to you if you want

Is this kind of what you are after?? Hope it helps some

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.