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? :|

8
  • 1
    As an aside, Date.now() already returns a number, so no need to use the + operator. Commented May 2, 2017 at 7:36
  • So... what should I do? I really need the Server time... (but not static, with php...) Commented May 2, 2017 at 7:40
  • So do you need a time based on specific time zone? Commented May 2, 2017 at 7:42
  • Yes. Europe/Bucharest. Commented May 2, 2017 at 7:42
  • 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. Commented May 2, 2017 at 7:50

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

5 Comments

So... I should put this in my script (at the beggining) and it should work?
@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.
Ok. Let's hope it will work. I'll come back with an up vote... :D
Can you check on www.Best11.org ? (it should be 11:11)
@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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.