Display Own Server Time (Not That of Visitor)

Hi

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’

Any suggestions?

thanks ever so much!
Karen

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

You’ll need a server-side script, in PHP, Python or similar, to grab the server’s time.

Edit: You could also use SHTML

1 Like

Hi

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 want to keep my server calls few

not by PHP. In JS you would update the time by means of setTimeout() or setInterval().

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.

Do you want to get the time dynamically? I mean to change every passed sec or minute or you want to be static?

Change dynamically

index.html file

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <title></title>       
    </head>
    <body>
        <div id="time"></div>
        <script>
        setInterval(function (){
         $.ajax({
       method: "POST",
       url: "timePHP.php",
       data: { time: "trigger" }
        })
       .success(function( msg ) {           
           $('#time').text(msg.slice(1, -2));        
       });  
        },1000);
        
        </script>
    </body>
</html>

timePHP.file

<?php

if(isset($_POST['time'])){
$current_date=array();
$info = getdate();
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];

array_push($current_date, "$hour:$min:$sec");
echo json_encode("$hour:$min:$sec");
}
        ?>

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

Thanks!!

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.

thanks again,
Karen

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.