ajoo Posted August 9, 2013 Share Posted August 9, 2013 Hi all code Gurus I am writing a small user login routine. It checks when a user has logged in and finished a task. The user can login multiple times before the task is over. However once its over the user can login only the next day. That is only once a new day begins. But when the user tries to login again after the task is over, it needs to tell the user after how many hours and minutes the user may login again. I am storing the login time as datetime formatted (Y-m-d H:i:s). So suppose the user logged in today at 4.00 PM and finished the task , the user can then login after say 00.01 AM or after its 1 minutes past the new day. Since that time, if the user logs in again, he / she needs to be told after how much time the new login can occur. Please can someone help me with this. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/280983-time-difference-between-login-time-next-date/ Share on other sites More sharing options...
denno020 Posted August 9, 2013 Share Posted August 9, 2013 Just a thought, you're going to want to use some javascript as well so you can get the local time for the user.. So the page loads, send a quick ajax back to your php script, which then returns the time interval until their midnight time, not the server midnight time (if that makes sense). Using javascript, you can do something like this: var currentTime = new Date(); var hours = currentTime.getHours(); var minutes = currentTime.getMinutes(); Then send hours and minutes back to the php script. I would think the easiest way to work out he time until midnight would be to subtract (hours*60+minutes) from (24*60) -which represents the total number of minutes in a day. Then from php return that total number of minutes, or return a string formatted with hours/minutes, or however you want to display it.. I haven't tried this, it's just an idea of something you could try. If you're struggling getting it going I can write up the code for what I'm trying to explain and show you that instead. Denno Quote Link to comment https://forums.phpfreaks.com/topic/280983-time-difference-between-login-time-next-date/#findComment-1444132 Share on other sites More sharing options...
cyberRobot Posted August 9, 2013 Share Posted August 9, 2013 Just a thought, you're going to want to use some javascript as well so you can get the local time for the user.. So the page loads, send a quick ajax back to your php script, which then returns the time interval until their midnight time, not the server midnight time (if that makes sense). If you decide to go with a JavaScript solution, just be aware that the user could modify their system's clock to cheat the system. Quote Link to comment https://forums.phpfreaks.com/topic/280983-time-difference-between-login-time-next-date/#findComment-1444143 Share on other sites More sharing options...
ajoo Posted August 9, 2013 Author Share Posted August 9, 2013 Hi guys, Thanks for the reply. I agree with cyber robot about the users who maybe able to cheat by changing the system clock so i prefer to use the server time instead. I am sure there would be ways in php, & yes I want to use only php and not java script ( having spent like 4 months to get the hang of php I don't wanna digress into javascript - nothing against javascript otherwise !! ), to make adjustments for the local time of the user. So can you or anyone suggest how i can do this in php. I need the code for time bcos i find the timestamp and conversions a lil tuff to program, there being so many ways to do a thing like this. I know it has to be sth like as is suggested by denno020: In terms of php $lastLogout = date("Y-m-d H:i:s"); if (task over) {$nextValidLogin = date(Y-m-d 24:00:00);} $diff = $nextValidLogin - $LastLogout; I hope I am correct and if I am then please some Guru may help me code this. Thanksssssss ! Quote Link to comment https://forums.phpfreaks.com/topic/280983-time-difference-between-login-time-next-date/#findComment-1444149 Share on other sites More sharing options...
PaulRyan Posted August 9, 2013 Share Posted August 9, 2013 Allow the user to select their timezone and then use that timezone to define when they can next do the task etc. Quote Link to comment https://forums.phpfreaks.com/topic/280983-time-difference-between-login-time-next-date/#findComment-1444169 Share on other sites More sharing options...
ajoo Posted August 10, 2013 Author Share Posted August 10, 2013 (edited) How can I know what my timezone is ? I have apache server installed on my machine and the time that my Apache server gives me is different from the system time on my machine. To be exact. The time showing on my machine is 12.50 PM on 10th Aug, 2013. but the time given to me by the Apache server is 2013-08-10 09:20:45 where the time is 9.20 AM. So it shows a time difference of about 3.30 hours. How can I resolve this? Where should I make the changes to adjust the server time correctly? Ok nevermind this one, I got it. <?php $timezone = "Asia/Calcutta";if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);echo date('d-m-Y H:i:s');?> Next I'll do the time calculations. Thanks all ! Edited August 10, 2013 by ajoo Quote Link to comment https://forums.phpfreaks.com/topic/280983-time-difference-between-login-time-next-date/#findComment-1444277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.