cloudll Posted April 3, 2016 Share Posted April 3, 2016 (edited) I am trying to make a php timer of sorts. For a city building game web app. I am using this code as a sort of time stamp but its very unreliable and jumpy. When a player clicks on a building to build, it stores the following as a session variable: $startTime = number_format($Time, 0, '.', ''); the to get the current time: $Time = date("his"); then to see if the 30 second build time has passed I use: if (isset($_SESSION['buildStart']) && ($Time - $_SESSION['buildStart'] > 30)) { built(); } the rest of the code works fine, but sometimes the timer will jump from say 5 seconds to 25 seconds in an instant. Does anyone have a more reliable way to check if the 30 seconds has passed? Sorry if this is all crappy code, I am tryng to make this to get better Edit: Oh, I don't think I can use time() because I am using the times to also make a progress bar: Edited April 3, 2016 by cloudll Quote Link to comment https://forums.phpfreaks.com/topic/301146-php-timer/ Share on other sites More sharing options...
Solution Jacques1 Posted April 3, 2016 Solution Share Posted April 3, 2016 The whole approach sounds rather weird. You've implemented a timer with pure PHP? How does that work? Timers are classical client-side features, i. e. a task for JavaScript. Of course you can and should also store the time on the server to prevent cheating, but all the visuals should be done entirely with JavaScript. Since you're storing the time in the session, what happens when the session is destroyed before the building is finished? Does it simply disappear? You'll probably want to use the database instead. And why exactly do you have to use this weird date("his") stuff instead of a proper timestamp? Do use time()! Quote Link to comment https://forums.phpfreaks.com/topic/301146-php-timer/#findComment-1532769 Share on other sites More sharing options...
cloudll Posted April 3, 2016 Author Share Posted April 3, 2016 I switched to time() and it all worked well, thanks I plan to change it from sessions to a database, I just used sessions when testing it on my laptop. I don't know why I thought I couldn't use that in the first place Is there a way to make time() a little easier to read ? Quote Link to comment https://forums.phpfreaks.com/topic/301146-php-timer/#findComment-1532770 Share on other sites More sharing options...
Jacques1 Posted April 3, 2016 Share Posted April 3, 2016 For simple formatting, there are date() and strftime(), both of which accept a Unix timestamp as a second argument. For more complex operations, there's the DateTime class which can be constructed from a Unix timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/301146-php-timer/#findComment-1532771 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.