korsohill Posted July 3, 2009 Share Posted July 3, 2009 I have made a simple website where a user can enter a username and password, and click a button. Once they're "logged in" the time is logged (with PHP Date "F d h:i:s a") and written to a database. Then when they "log out" (by entering their username and password again) it records their time again.On the main page of the site, it shows who is logged in, the time they got on, and, hopefully, how long they've been on. What I'm unsure of is how to make a script that will pull their time from the database, figure out the difference (also in "F d h:i:s a") between when they signed in and now) and then display that as well.I'm doing this for a "No Sleeping Contest". Any questions? Link to comment Share on other sites More sharing options...
AElliott Posted July 3, 2009 Share Posted July 3, 2009 Not sure what you mean by having the same format for the difference, because that's not a date. Use something like this (key function here strtotime): <?php// assuming we already have the times as $loginTime and $logoutTime$diff = strtotime($logoutTime)-strtotime($loginTime);// we now have the number of seconds they were logged in in $diff// we can format that into hours/minutes/seconds etc if required. What format did you want??> Link to comment Share on other sites More sharing options...
justsomeguy Posted July 3, 2009 Share Posted July 3, 2009 Keep in mind you're guaranteeing that people will actually click the login button, if they're just inactive and their session times out, or if they close the browser or go to another page, you wouldn't pick that up. You can use ajax if you want to avoid a session timeout. Link to comment Share on other sites More sharing options...
korsohill Posted July 3, 2009 Author Share Posted July 3, 2009 If I can just have it as Day(s), Hour(s), Minute(s), Second(s), that would be great.Also, since I'm pulling the times form the DB as any array, how can I make this work? Or is there a better way to get them from the DB? Link to comment Share on other sites More sharing options...
korsohill Posted July 3, 2009 Author Share Posted July 3, 2009 Keep in mind you're guaranteeing that people will actually click the login button, if they're just inactive and their session times out, or if they close the browser or go to another page, you wouldn't pick that up. You can use ajax if you want to avoid a session timeout.What do you mean? Once they click "Login" it enters their time in a DB and it won't matter if they leave the page... Link to comment Share on other sites More sharing options...
justsomeguy Posted July 3, 2009 Share Posted July 3, 2009 Oh right, you're having them "log out" by entering their username and password again. I get it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.