So I'm making a gaming where 2 people play a game, report their scores on the website, and the winner gets credited. Now I want to add a feature where if one person reports a result and the opponent fails to report his own result within 5 minutes, the website will automatically credit the person who reported the result.
Currently, I have coded a countdown timer in PHP and Javascript (and I prefer to do the countdown in Javascript and call the PHP function after the time elapse but it looks like I can't execute PHP code within Javascript but I would like to do this if possible).
I have equally written the countdown in PHP but it looks like PHP can't run in the background, so it's not updating the balance even after the time elapse.
Here's the PHP countdown as an example of what I want to do.
<?php
// Set the date we're counting down to
$countDownDate = strtotime('Dec 25, 2020 15:37:25') * 1000;
//Get current date
$now = time() * 1000;
// Find the distance between now an the count down date
$distance = $countDownDate - $now;
// Time calculations for days, hours, minutes and seconds
$days = floor($distance / (1000 * 60 * 60 * 24));
$hours = floor(($distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
$minutes = floor(($distance % (1000 * 60 * 60)) / (1000 * 60));
$seconds = floor(($distance % (1000 * 60)) / 1000);
// Output the result
echo days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
// If the count down is over, update user balance.
if ($distance < 0) {
$mysqli->query("UPDATE users SET bal=bal+50");
}
?>
The problem that this code doesn't update the balance in real-time after the time elapsed. It updates the balance each time the page is reloaded and keeps updating the balance non-stop.
WHEN I change the if the part to
if($distance ==0)
then it will not update at all because PHP is not doing a background check to know when the counter reaches zero and update the balance.
If you understand what I want to do please suggest how I can do this checking and auto-updating. I will also appreciate the sample code.
Thank you.