Hi,

I'm making a text based online game, and I need to make a timer that does something every 60 min. I have 2 questions:
1) Is it possible to make a countdown that resets after every "run" and restarts
2) To make a PHP script for this, will I need to keep a browser window with this script open on the server 24/7?

Thanks, Nick

    There are a bunch of ways to do it, and none of them are very nice...

    For a timer thing like this its often better to use some kind of external service to keep track of the time. If you're on windows, make a COM object that runs as a windows service and then just query that for the time with PHP. You can do similar things with Linux though I don't know the specifics. This is assuming you need a central timer, like for the duration of the game.

    Another way to do it, though less percise, is to store the start time of the timer in a database or something, and then anytime a player loads the page just subtract time() - $timerStartTime and send that to the browser. This certinally isn't real time, but then again your game probably isn't either.

    You also have the window.setTimeout() javascript function, that would allow each player's browser to keep track of its own timer. Anytime you do this though you introduce the risk of someone fudging the data that's sent to the server that could mess things up.

    If you come back with some more specifics maybe me or someone else could help you out more.

    Erestar

      Thanks for for quick reply. I was suggested the same thing on another forum, so I guess I'm gonna go for a cronjob on this linux server, seems like the best idea.
      Another question, what should I set the cronjob to do? It' cant just run php scripts, maybe run it through the PHP parser, is this even possible?

        Well, the idea that the timer needs to be "running" means that a PHP script probably isn't the best thing to use.

        You never want to have your timer object just being a loop that runs until the total time is reached, as that's pretty bad for the processor.

        I'm not quite sure what the correct way to do this would be.

        How percise does your timer need to be?

        The only thing I can think of is writing your own timer server that keeps seperate threads for each timer you create and accessing it through some socket with PHP.

        Again, if you reveal the purpose of this timer, maybe we can get a little further with figuring out how it should work and what you can get away with.

          Originally posted by nickk

          Another question, what should I set the cronjob to do? It' cant just run php scripts, maybe run it through the PHP parser, is this even possible?

          FYI, in windows there's a CLI php executable that's used for parsing PHP functions from a command line. If this exists in Linux you could have your cron job call this CLI thing, passing anypage you want processed, and it'll do it as though it were accessed as a web request (for the most part).

            Cool thanks Erestar, I'l look into that. The purpous of this timer is to give users a certain amount of "money" (points) every hour.

              Oh jeez! Is that all? Yeah, just run a cron job php script that reads all your players and updates their accounts.

              Even if you can't get the PHP stuff to work with the cron job (which you probably can) its really easy to write a little java app that can talk to your database and do the same thing.

              Wish you would have said that earlier :-p

              Erestar

                Write a Reply...