2

I have a website (www.Best11.org) and I tried to put the server time in the footer with JavaScript. I thought everything is fine, but some users told me that it's local time actually, instead of server time...

Here's the script...

<script type='text/javascript'>
    var serverTime = <?php echo time() * 1000; ?>;
    var localTime = +Date.now();
    var timeDiff = serverTime - localTime;

    setInterval(function () {
        var realtime = +Date.now() + timeDiff;
        var date = new Date(realtime);
        // hours part from the timestamp
        var hours = date.getHours();
        if (hours < 10)
            hours = "0" + hours
        var minutes = date.getMinutes();
        if (minutes < 10)
            minutes = "0" + minutes
        var seconds = date.getSeconds();
        if (seconds < 10)
            seconds = "0" + seconds

        var formattedTime = hours + ':' + minutes + ':' + seconds;
        document.getElementById('clock').innerHTML = formattedTime;
    }, 1000);
</script>

What's wrong? :|

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
razvee
  • 33
  • 4
  • 1
    As an aside, `Date.now()` already returns a number, so no need to use the `+` operator. – nnnnnn May 02 '17 at 07:36
  • So... what should I do? I really need the Server time... (but not static, with php...) – razvee May 02 '17 at 07:40
  • So do you need a time based on specific time zone? – Shota May 02 '17 at 07:42
  • Yes. Europe/Bucharest. – razvee May 02 '17 at 07:42
  • What I've meant is... Every user from different countries, sees the time from his country... And I wanted to display the Server Time (Europe/Bucharest). – razvee May 02 '17 at 07:46
  • You can use the accepted function from here: http://stackoverflow.com/questions/8207655/how-to-get-time-of-specific-timezone-using-javascript. Or use moment-timezone – Shota May 02 '17 at 07:48
  • 1
    @StephenC UNIX timestamps (seconds since 1970) are always in UTC, no matter what local timezone the computer is set to. That's by definition. If the hardware clock is using the wrong timezone somewhere somehow that means its clock is simply *universally wrong*, which the server offset calculation is supposed to remedy I'd think. – deceze May 02 '17 at 07:50
  • Have you never come across a Windows system where the hardware clock was incorrectly set to local time? I don't think that the offset calculation will correct that. – Stephen C May 02 '17 at 11:38

1 Answers1

1

time returns the Unix timestamp. So what you're doing is showing the server's clock time on the client, but in their timezone.

If you want to show the server's current time, you need to use server local time for the value you pass to the JavaScript code. In this example, I've used getdate to get the local date/time on the server, and then built a call to the Date constructor for the client-side code, to get the offset between them:

var serverTime = new Date(
    <?php $now = getdate();
          echo $now['year']  . ',' . ($now['mon'] - 1) . ',' . $now['mday'] . ',' .
               $now['hours'] . ',' . $now['minutes']   . ',' . $now['seconds'];
?>);
var localTime = Date.now();
var timeDiff = serverTime.getTime() - localTime;

The rest of the script is unchanged.

I don't do a lot of PHP, I wouldn't be surprised if you can get your current timezone offset and do the math on the value from time instead. But the fundamental answer is that you weren't allowing for the server's timezone in what you sent the client, which is why it seemed to show the client's time (just according to the clock of the server).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • So... I should put this in my script (at the beggining) and it should work? – razvee May 02 '17 at 07:59
  • @razvee: Yes, provided you've told PHP what your server's timezone is. Just replace the three lines the above clearly replaces in your script. – T.J. Crowder May 02 '17 at 08:00
  • Ok. Let's hope it will work. I'll come back with an up vote... :D – razvee May 02 '17 at 08:02
  • Can you check on www.Best11.org ? (it should be 11:11) – razvee May 02 '17 at 08:12
  • @razvee: Says 11:33 for me, which sounds right as I'm writing this ~22 minutes after your comment above. :-) (Whereas it's 9:33 for me here in the UK.) So it sounds like you're good! – T.J. Crowder May 02 '17 at 08:33