I found the following function at how to countdown to a date
However, it is using the local machines time. This is not applicable to my case. It needs to be the server time.
I would like to pass the time to the JS from PHP, and then for this script to count on the remaining days, hours, minutes and seconds.
All the script above does is call a function at a set interval, then keep fetching the local time. Is it possible for it to count down on its own without getting the local machines time? What would that look like?
Is it better to use Javascript or jQuery for this?
CountDownTimer('08/19/2018 10:01 AM', 'countdown');
CountDownTimer('08/20/2017 10:01 AM', 'newcountdown');
function CountDownTimer(dt, id) {
var end = new Date(dt);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
document.getElementById(id).innerHTML = 'EXPIRED!';
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
document.getElementById(id).innerHTML = days + 'days ';
document.getElementById(id).innerHTML += hours + 'hrs ';
document.getElementById(id).innerHTML += minutes + 'mins ';
document.getElementById(id).innerHTML += seconds + 'secs';
}
timer = setInterval(showRemaining, 1000);
}
<div id="countdown"></div>
<div id="newcountdown"></div>