How should I prepare the database of the counter counting down in seconds with php?

Viewed 21

I'm trying to set up counter logic for a game. The critical point here is that every user who enters this page sees the same second at that moment. I will have a counter that counts down from 25 and everyone will place their bets during this time. When the 25 seconds are over, a 10 second counter will start, during which 10 seconds the winning bet will be announced. How can I set up the database setup so that these counters are repeated continuously?

I am using the following code for javascript, but every time I enter the page it starts from 25

var interval = 25000;
var interval1 = 10000;
var endTime;

function millisToMinutesAndSeconds(millis) {
    // Use floor instead of toFixed
    var seconds = Math.floor((millis % 60000) / 1000);
    return (seconds < 10 ? "0" : "") + seconds;
}

function reset() {
    // Use Date.now() instead of +new Date()
    // And create a cycle length that covers both intervals
    endTime = Date.now() + interval + interval1;
}

reset();
setInterval(function () {
    var remaining = endTime - Date.now();
    if (remaining >= 0) {
        // Adjust the time to display 
        // depending on where in the total interval we are:
        if (remaining >= interval1) remaining -= interval1;
        document.getElementById("timer").innerText =
            millisToMinutesAndSeconds(remaining);
    } else {
        reset()
    }
}, 100);
1 Answers

You just need to put the end date and time in database. The countdown must be done with javascript.

Get the date from backend (database) with php and pass it to javascript. Then do the countdown with js.

This tutorial seems exactly like what you need.

Related