I have a JS code that displays a live digital clock, correcting the minutes/seconds from the server, so if one of the clients has some minutes more or less, it corrects them to match the server time.
<script>
var serverTime = <?php echo (int) round(microtime(true) * 1000) ?>;
var localTime = +Date.now();
var timeDiff = serverTime - localTime;
$(document).ready(function() {
clockUpdate();
setInterval(clockUpdate, 1000);
});
function clockUpdate() {
var t = +Date.now() + timeDiff
var date = new Date(t);
$('.digital-clock').css({'color': '#fff', 'text-shadow': '0 0 0px #ff0'});
function addZero(x) {
if (x < 10) {
return x = '0' + x;
} else {
return x;
}
}
function twelveHour(x) {
if (x > 12) {
return x = x - 12;
} else if (x == 0) {
return x = 12;
} else {
return x;
}
}
var h = addZero(twelveHour(date.getHours()));
var m = addZero(date.getMinutes());
var s = addZero(date.getSeconds());
$('.digital-clock').text(h + ':' + m + ':' + s)
}
</script>
Now the script works as expected but I realized that when one of the clients is set to a different TimeZone, the clock will match the hour on the client,
If now on the server is 14:52 and on the client is 15:52, it will display 15:52 and I want the opposite, I want all the clocks displayed on all the clients match the exact time on the server (14:52).
Please help me just modify things in this code, no need for other online examples.
/*****************/
Thanks for all who took it seriously and tried to help (not like those who are ready waiting just to vote for closing the question the same second I asked it)
I want to explain it more:
The server is located in France and is using GMT+1 with DST
The clients are in Tunisia, the same timezone GMT+1 but DST is not applicable. BUT for some reason many clients in Tunisia use DST being unaware maybe.
So that's why the time on the page must be the exact same time as in the server and everywhere else.
So with PHP I'm using date_default_timezone_set('Africa/Tunis');
to set the server time to Tunisia timezone.
Thanks!