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

后端 未结 5 1159
再見小時候
再見小時候 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:26

    server_time.js

    $.ajax({
            type: "POST",
            data:"",
            url: "ajax-get-server-time.php",
            success: function(result){
                    $("#time_div").html(result);
                }
            });
    

    ajax-get-server-time.php

    <?php
    date_default_timezone_set(**YOUR DEFAULT TIMEZONE**); //e.g. date_default_timezone_set('Asia/Kolkata');
    echo date("H:i:s A");
    ?>
    
    0 讨论(0)
  • 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

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

    0 讨论(0)
  • 2020-12-29 09:27

    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>

    0 讨论(0)
  • 2020-12-29 09:31

    This example will try to display server time every second.

    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    
    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);
    
    function display_c(){
    var refresh=1000; // Refresh rate in milli seconds
    mytime=setTimeout('display_ct()',refresh)
    }
    
    function display_ct() {
    var strcount
    var st = srvTime();
    var x = new Date(st);                       //new Date()
    document.getElementById('ct').innerHTML = x;
    tt=display_c();
    }
    </script>
    </head>
    
    <body onload=display_ct();>
    <span id='ct' ></span>
    
    </body>
    </html>
    

    SOURCE1

    http://www.plus2net.com/javascript_tutorial/clock.php Note the above example is php free.

    SOURCE2

    http://www.webdeveloper.com/forum/showthread.php?228309-Getting-server-date-time-with-no-server-side-script

    0 讨论(0)
  • 2020-12-29 09:35

    Funny enough, while looking for information about Codeigniter to see if I could answer your question or not, I found a runnable that does exactly what you need:

    http://runnable.com/UXczcazDrMMiAAGl/how-to-do-ajax-in-codeigniter-for-php

    In short, the above script uses Codeigniter and jQuery to ask the server for its current time (via AJAX).

    Happy coding.

    0 讨论(0)
提交回复
热议问题