I am a little new to Javascript. I am looking for some code to display the time on our own servers (UTC) when a visitor comes to our website.
In otherwords, the UTC time is (ping our own server) however, I would like the simple time HH:MM to continue to run - not to have to ping the server each time like with PHP.
Something like ‘The time on our own servers is now HH:MM’
you need to make your server print the time directly (as JS doesn’t know the server’s time).
if all you want is the UTC time, it suffices to use Date’s UTC methods (although that implies that both the client time and the server time are correct).
I can do that - and display the time initially. But, is there a lightweight way to increment the time by minutes either using Javascript or PHP (that only calls the server once?).
I certainly would not do page refreshes.
And I would not do AJAX calls for this.
I would pass the PHP value into the JavaScript eg.
var server_time = "<?php echo $server_time; ?>";
The time might be within plus or minus one minute of the actual server time depending on when the minute “kicks over” but I’m guessing that should be good enough and would limit the number of calls to the server.
Any time you get from the server will already be outdated when it arrives at the client… I think the easiest approach would be to just send the timezone offset, and calculate the server time on the client from then on.
Otherwise you’d have to send constant requests to the server as @liontas76 suggests, but then you’d also have to measure the time the request took, calculate the difference, and schedule the requests in a way such that the updates are guaranteed to happen in time and in the correct order. This might be done using for example observables, but really, this seems a bit like overkill… I’d suggest to just go with the offset. :-)
Looking at it, I think that I will bury it a little deeper in our create_meeting page or - perhaps when I can figure out how to get it on the nav bar, maybe in the upper right hand corner.
But, I love it!!
ps - This episode has reminded me that I need to learn JavaScript.