2 Replies - 747 Views - Last Post: 23 December 2012 - 01:32 PM Rate Topic: -----

#1 AnonymousPartner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-December 12

Need Help Coding A Timer In PHP? (PLEASE RESPOND)

Posted 23 December 2012 - 12:33 PM

I've been trying and trying to code a timer in PHP for on line timer based game, but I can't figure it out. I've searched all over the Internet for a solution, but can't find anything.

I need to code a timer that will give you a certain amount of experience for a certain action, and the timer needs to run on a certain length for that certain action.

SO, for example. Say Your mining, for sake of the thread, it takes 30 seconds to mine 1 copper ore. Mining that one copper ore in that 30 second time period gives 13 experience. Any thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help Coding A Timer In PHP? (PLEASE RESPOND)

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5226
  • View blog
  • Posts: 14,006
  • Joined: 18-April 07

Re: Need Help Coding A Timer In PHP? (PLEASE RESPOND)

Posted 23 December 2012 - 12:49 PM

There is no timers in the traditional sense. Remember that PHP is a server side language where it handles a request, runs some code and generates a response then forgets about it.

So you have two options here. You keep track of a timestamp of the last request and when a new request comes in you subtract that timestamp from the current time and that will tell you how much time elapsed. This is how time that passed in the world can be accomplished between logins too... I login, do stuff, logout. You keep track of the world time at the time of logout. When I login again, I take the current time and subtract the time of their last logout to give me the time that has passed in the world.

The second option available to you is, if this is a linux machine, is running cron jobs. In other words, the schedule task manager that you can use to run system PHP scripts to upkeep the world resources. So for instance every 5 minutes you run a script that determines what has happened in the world in the last 5 minutes, then credits players X number of coins, lumber, stone etc.

Sometimes a combination of the two of these can keep time running in a game. But again, PHP itself doesn't have your typical timer since it runs then quits after the script is finished.

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,563
  • Joined: 12-December 12

Re: Need Help Coding A Timer In PHP? (PLEASE RESPOND)

Posted 23 December 2012 - 01:32 PM

A possible alternative is to inject a value into a Javascript variable.

echo "var tLimit = $tvalue;";

then use this value as the limit for a setTimeout, which might then trigger a page re-direct or an Ajax request (if needed). But, of course, the user could easily modify the interval.

This post has been edited by andrewsw: 23 December 2012 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1