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

后端 未结 5 1158
再見小時候
再見小時候 2020-12-29 08:46

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

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 09:27

    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

    
      
        
        Server date/time
        
      
      
      
      
    

    Cheers!!

提交回复
热议问题