Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to get Web Server time and display it on Web Pages

I am developing my website using PHP Codeigniter Framework. I want to display my webserver's time as well as client's machine time on some of my web pages. I successfully displayed client side time using JavaScript that updates the time every second by simply using setInterval('clientMachineTime()',1000) function. I want to display the web server time with the same working of my client side clock. I googled for this but could not find exactly what i am looking for. Any suggestion would be appreciated. Thanks.

like image 544
d_4dinkey Avatar asked Nov 28 '13 15:11

d_4dinkey


People also ask

How do I get the time on my web server?

Visit the Web Page of the server you want to check. Select the entry and take a look at the HTTP Response Headers, you're looking for the one called 'Date' which contains the local time as well as the time zone of the server.

How do I get the date and time to show on my website?

Javascript date object and HTML span element can be used to display the current date and time. By default Javascript use the browser's timezone to display time and date.

How do I set the current time in HTML?

var date = new Date(); var currentDate = date. toISOString(). slice(0,10); var currentTime = date.

How do I get server time in react?

we will use Date() to get current timestamps and then display date and time in react js app.


2 Answers

Answering my own question

I found what I was looking for on Webdeveloper.com and it worked excellently for me.

serverDate.js

var xmlHttp;
function srvTime(){
    try {
        //FF, Opera, Safari, Chrome
        xmlHttp = new XMLHttpRequest();
    }
    catch (err1) {
        //IE
        try {
            xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (eerr3) {
                //AJAX not supported, use CPU time.
                alert("AJAX not supported");
            }
        }
    }
    xmlHttp.open('HEAD',window.location.href.toString(),false);
    xmlHttp.setRequestHeader("Content-Type", "text/html");
    xmlHttp.send('');
    return xmlHttp.getResponseHeader("Date");
}

var st = srvTime();
var date = new Date(st);

html

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Server date/time</title>
    <script language="javascript" src="serverDate.js"></script>
  </head>
  <script language="javascript">
  var localTime = new Date();
  document.write("Local machine time is: " + localTime + "<br>");
  document.write("Server time is: " + date);
  </script>
  <body>
  </body>

Cheers!!

like image 189
d_4dinkey Avatar answered Oct 02 '22 00:10

d_4dinkey


function getServerTime() {
  return $.ajax({async: false}).getResponseHeader( 'Date' );
}
console.log('Server Time: ', getServerTime());
console.log('Locale Time: ', new Date(getServerTime()));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
like image 21
Marcelo Ribeiro Avatar answered Oct 01 '22 22:10

Marcelo Ribeiro