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

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


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!!


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>