I am creating a countdown script, there is a PHP admin interface where a user can enter the date/time to countdown to, the admin will input the date/time in their local timezone and this gets passed into the script.
For users who visit the website in a different timezone I want a countdown to the date/time based on the admin's timezone not the users.
e.g.
- Admin enters a date/time of 2023-06-01 13:00:00 and is in London
- Website visitor from London on 2023-06-01 12:00:00 will see an hour countdown, website visitor from San Fransisco will see the same hour countdown
My code so far with comments for further explanation:
<div id="countdown"></div>
<script>
function countdown() {
var countdown = document.getElementById('countdown');
var countdownDate = new Date('2023-06-28 10:00:00').getTime(); // The date and time here is being populated by a PHP echo, the date and time getting echoed is input via an admin interface and would be entered by the admin in their local timezone
// The issue is that if a user of the website is in a different timezone to what the dates/times was entered in then the countdown will be higher/lower due to it being based on the users timezone. I just want the countdown to be to the date/time based on the timezone they were entered in. I could add something into the admin interface to enter a timezone string the dates entered are based on, then pass that to the JS to manipulate the dates? Or maybe there is another solution?
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countdownDate - now;
if (distance < 0) {
clearInterval(x);
countdown.remove();
} else {
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
var countdownText = '';
countdownText += days + ' days ';
countdownText += hours + ' hours ';
countdownText += minutes + ' mins ';
countdownText += seconds + ' secs';
countdown.innerHTML = countdownText;
}
}, 1000);
}
countdown();
</script>