I have a PHP script set up to regenerate a game character's statistics(health, energy, etc...). The approach I'm using is to store the result of a call to time() along with the character's data, and then when I pull the data out of the database I subtract the time of the last update from the current time, e.g:

Last update: 1264322000
Current time: 1264322357
Time elapsed: 357 seconds

That part works, the problem is the second part of the script:

$regen_remainder = $this->regen_secs - ($this->regen * 60);

echo "Regen remainder: " . $regen_remainder . "<br />"; // check value
echo "Current time: " . $this->current_time . "<br />"; // check value		

$this->lastupdate = $this->current_time - $regen_remainder;

echo $this->lastupdate;  // check value

The results of the three "echo" statements are as follows:
Regen remainder: 57
Current time: 1264322357
1.2643223E+09

It always produces a completely different number(usually +~40 instead of -whatever), messing up the next few calculations. I can't seem to figure out why it's doing this. Any ideas?

Recommended Answers

All 2 Replies

The results of the three "echo" statements are as follows:
Regen remainder: 57
Current time: 1264322357
1.2643223E+09

This is a limitation in the computing world (64 and 32 bit processors). They can only store so many numbers before showing all weird things. Fortunately php has a way around this. When doing math for large numbers try using the bcmath library. This will store the numbers as strings and process the numbers as strings. Then you may have infinit sized numbers. If you like post your full code and I shall try to implement it for you.

Ahh, that makes sense. I tried it using bcsub() to do the subtraction and it worked perfectly. Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.