简体   繁体   中英

How to make a countdown timer that automatically restarts using PHP

I have a requirement to make a countdown timer that automatically restarts when the time has elapsed.

For example, I need to countdown from 3pm or 15:00 UK time and reset to start counting again when the time has reached.

I've been trying with some jQuery but that will show browser time and not server time. If anyone is able to share a solution please let me know.

Thanks in advance!

The code below is a working example from which works perfectly: https://gist.github.com/Majestik/3964527

if (document.getElementById('countdownTimer')) {
    pad = function(n, len) { // leading 0's
        var s = n.toString();
        return (new Array( (len - s.length + 1) ).join('0')) + s;
    };
    function countDown() {
        var now = new Date();
        if ( (now.getDay() >= 1) && (now.getDay() <= 5) ) { // Monday to Friday only
            var target = 15; // 15:00hrs is the cut-off point
            if (now.getHours() < target) { // don't do anything if we're past the cut-off point
                var hrs = (target - 1) - now.getHours();
                if (hrs < 0) hrs = 0;
                var mins = 59 - now.getMinutes();
                if (mins < 0) mins = 0;
                var secs = 59 - now.getSeconds();
                if (secs < 0) secs = 0;
                var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
                document.getElementById('countdownTimer').innerHTML = str;
            }
        }
    }
    var timerRunning = setInterval('countDown()', 1000);
}

 Time remaining: <span id="countdownTimer"><span>00:00.<small>00</small></span>

在编写包含此javascript文件的HTML的PHP​​代码时,只需在其中设置变量,而不是让javascript一旦加载就可以执行此操作,这样,您将使用服务器时间而不是浏览器时间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM