Jump to content

php timer


cloudll
Go to solution Solved by Jacques1,

Recommended Posts

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 :P

 

Edit: Oh, I don't think I can use time() because I am using the times to also make a progress bar:

Edited by cloudll
Link to comment
Share on other sites

  • Solution

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()!

Link to comment
Share on other sites

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 :P

 

Is there a way to make time() a little easier to read ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.