0

Okay so i need a countdown to the servers midnight because when the server reach midnight it will run a cronjob which is relevant to the users so they need to see how many hours that are left untill it is midnight on the server and not in their own timezone.

Sample page here

The date_default_timezone_get(); is europe/copenhagen

PHP

(The page the Ajax is requesting)

echo time() * 1000;

Javascript

            (function () {
                var test = document.getElementById("test");
                var difference = document.getElementById("difference");

                var serverMilli = document.getElementById("serverMilli");
                var serverCountdown = document.getElementById("serverCountdown");

                var machineMilli = document.getElementById("machineMilli");
                var machineCountdown = document.getElementById("machineCountdown");

                let serverTime;
                var localTime;

                var http = new XMLHttpRequest();
                var url = "time.php";
                http.open("POST", url, true);
                http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                http.onreadystatechange = function () {
                    if (http.readyState === 4 && http.status === 200) {
                        serverTime = this.responseText;
                    }
                    ;
                };
                http.send();

                function countDownServer() {

                    var now = new Date();
                    var localTime = now.getTime();
                    var currentDiff = serverTime - localTime;
                    var currentTest = serverTime - currentDiff;

                    var currentTime = currentTest;
                    var eventDate = new Date();
                    eventDate.setDate(now.getDate() + 1);
                    eventDate.setHours(24);
                    eventDate.setMinutes(0);
                    eventDate.setSeconds(0);
                    eventDate.setMilliseconds(0);

                    var eventTime = eventDate.getTime();
                    var remainingTime = eventTime - currentTime;

                    var sekunder = Math.floor(remainingTime / 1000);
                    var minutter = Math.floor(sekunder / 60);
                    var timer = Math.floor(minutter / 60);

                    sekunder %= 60;
                    minutter %= 60;
                    timer %= 24;

                    sekunder = (sekunder < 10) ? "0" + sekunder : sekunder;
                    minutter = (minutter < 10) ? "0" + minutter : minutter;
                    timer = (timer < 10) ? "0" + timer : timer;

                    var testServer = timer + ":" + minutter + ":" + sekunder;
                    serverCountdown.textContent = testServer;
                    setTimeout(countDownServer, 1000);
                }
                countDownServer();
                })();

Everything "kinda" works... the problem is if i change the timezone on my computer it will display longer hours untill midnight than it really is on the server? How is this possible when the timezone on server is europe/copenhagen and i use time() ? should it not use the servers timezone?

Javaish
  • 179
  • 14

2 Answers2

0

No as javascript executes in your browser, it will use the timezone of your computer.

You have to tell javascript which timezone you would like (if not your one)

I found this for you:

How to get Time of specific timezone using javascript?

Nila
  • 49
  • 10
  • I will look into it, but i dont understand why time() does not return the correct seconds for the servers timezone – Javaish Jun 03 '17 at 16:59
  • time() should be returning an UNIX timestamp http://php.net/manual/en/function.time.php so you need to calculate the value for your timezone – Nila Jun 03 '17 at 17:00
  • Seems more complicated than i first thought... well i will have to look into this : – Javaish Jun 03 '17 at 17:04
0

The JavaScript Date object always uses the user's computer time. You can use a library like Moment Timezone to set a different timezone that can be calculated on the client.

Otherwise you can try and calculate the difference between the server time and local time, but it might be off by an hour a couple times per year due to differences in DST.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • I will look into it, but when i use AJAX to get the servers time() * 1000 and use this for the rest of the javascript should it not be correct ? – Javaish Jun 03 '17 at 16:56
  • @Javaish If you pass the sever time to the client and create a `Date` object out of it, the date will be correct but the timezone will be different, meaning the client will see a different time of day, even though it represents the same moment in time. – mpen Jun 03 '17 at 17:01
  • Okay well i will have to study this a bit more.. this is confusing me alot and taking up too much time :/ – Javaish Jun 03 '17 at 17:05
  • @Javaish Just think about it. If I'm in Los Angeles and you're in New York, it can be 1:35pm here while simultaneously being 4:35pm there. Same moment in time for both of us, same unix timestamp, different numbers on our watches. – mpen Jun 03 '17 at 20:38