d_4dinkey d_4dinkey - 1 year ago 84
PHP Question

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
that updates the time every second by simply using
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.

Answer Source

Answering my own question

I found what I was looking for on Webdeveloper.com and it worked excellently for me. I want to share it because it was not easy to find. I hope it will be helpful for others.


var xmlHttp;
function srvTime(){
    try {
        //FF, Opera, Safari, Chrome
        xmlHttp = new XMLHttpRequest();
    catch (err1) {
        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.setRequestHeader("Content-Type", "text/html");
    return xmlHttp.getResponseHeader("Date");

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


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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download