Is there any way to catch up?

Asked

Viewed 55 times

0

I’m developing a little game in which the user will win 200 of money per minute.

I have a simple time comparison script ready, which saves the current time(+ 1 minute) in the database and compares with the actual current time.

$_SESSION['waterhour'] = new DateTime();
    echo $_SESSION['waterhour']->format('Y-m-d H:i:s') . "<BR>";
    $_SESSION['waterhourplus'] = new DateTime('+1 minutes');
     echo $_SESSION['waterhourplus']->format('Y-m-d H:i:s');

When the database time is equal to the present will update the user’s money.

if ($_SESSION['timewater'] == $_SESSION['waterhour']) {
            $objUpWater = new Resources();
            $objUpWater->WaterUpdate();
        }

So far so good, everything works correctly if the user keeps updating the page 1 in 1 minute, what I wanted to know is how to catch the time that has passed if the user does not update the page every 1 minute.

For example, when the user stays more than 5 minutes without updating the page the time will be (-4 minutes).

Got to catch these -4 minutes ? Since the minutes will be used as the amount of times the user will earn the money.

Obs: I didn’t want to use cron job, it wouldn’t work properly since each user would have seconds of differences in time to earn the value depending on the time of account creation and the value would end up not updating at the right time for everyone.

  • You can do this using javascript. setInterval is the function name.

  • But setInterval would only work if the user had the page tab open as well. The goal is to take the negative value of the time spent to give the user money in the correct amount

  • javascript e momentJS... no momentJS can contradict hours, hence only use the library adapting your need, any doubt comments here that I try to help better.

  • 1

    @Diêgocorreiadeandrade I had not yet heard about momentJS, I will research about it

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.