Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Time server

  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Time server

    How can i refresh a php page every second
    I have a time server which i want to display the time every second and having a hard time implementing this.

    Please help
    If you can design both the PHP code and javascript/Ajax to lad aparticular page every second then i will be glad

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    I have a time server which i want to display the time every second and having a hard time implementing this.
    Do this in javascript and you wont get your host mad for taking all the server time cycles.
    The JS code is all over, just google it.
    Evolution - The non-random survival of random variants.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,188
    Thanks
    10
    Thanked 569 Times in 550 Posts
    loading a url every second to display a php-generated clock is about the most wasteful use of ajax i've ever heard of...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You can have a timer where the initial time (from when the page is loaded) is from the server. Then use it as base for a javascript timer that increments every second.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,188
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by glenngv View Post
    You can have a timer where the initial time (from when the page is loaded) is from the server. Then use it as base for a javascript timer that increments every second.
    timers can drift according to cpu load. you actually want to calculate the difference between the server time and the client time, and add that to new Date() to get a Date object of the server's exact time. even these can drift over time as the OS updates the system clock from the internet time servers, but it's still a lot better than setInterval or hammer ajax...

    most servers spit out a Date header, which is required by http1.1. if that's the case, or even in some browsers, you can access the server time from javascript.

    for example, on this page in chrome's dev tools:
    Code:
    function serverTime(){
      return new Date(  
         new Date().getTime() +  
         ( new Date( document.lastModified ).getTime()  - performance.timing.responseStart )  
      );
    }
    
    serverTime()
    Last edited by rnd me; 09-26-2013 at 07:51 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Timezone not considered? From MDN, referring to document.lastModified, "WebKit returns the time string in UTC; Gecko and Internet Explorer return a time in the local timezone."

    But regardless, if you add client-side time into the equation, then there is a chance the timer will not be correct if the client-side time is not correctly set.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,188
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by glenngv View Post
    Timezone not considered? From MDN, referring to document.lastModified, "WebKit returns the time string in UTC; Gecko and Internet Explorer return a time in the local timezone."

    But regardless, if you add client-side time into the equation, then there is a chance the timer will not be correct if the client-side time is not correctly set.
    new Date() should figure out the format and timezone and act accordingly.
    my example was only tested in chrome, where it works well.

    it actually won't matter if the user's clock is off or not or set to 1988; as long as it itsn't manually changed after the page loads, the code i posted works just fine. the user's clock is just keeping track of seconds since page load...


    edit: ok, the date from lastModified is not a valid full date, so yeah, good point. i'll post an xbrowser copy after a meeting...

    edit2: a HEAD request gives a more reliable date format and works without window.performance, since you can just use +new Date() instead, which leaves only the http lag as an error margin... i saw a HEAD>date script on here the other day, don't recall where or why...
    Last edited by rnd me; 09-26-2013 at 09:00 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    If high precision is not critical and required, then I think this should suffice:
    Code:
    Server time: <span id="time"></span>
    PHP Code:
    var time = document.getElementById('time');
    function showTime(msec) {
      var d = new Date(msec),
        newTime = d.getTime();
      d.setTime(newTime);
      setTimeout(function() {
        showTime(newTime + 1000);
      }, 1000);
      time.innerHTML = d;
    }

    showTime(<?php echo round(microtime(true) * 1000); ?>);

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    But Glenn, we have demonstrated several times here that setTimeout and even setInterval can get many many seconds "off" from the correct time within a relatively short period.

    It really is worth the trouble to do this right:
    Code:
    <!doctype html>
    <html>
    <body>
    The server time is <span id="serverTime"></span>
    
    <script type="text/javascript">
    var offset = (new Date()).getTime() - <?php echo round(microtime(true) * 1000); ?>;
    
    function tick( )
    {
        var now = new Date();
        now.setTime( now.getTime() - offset );
        var hr = now.getHours();
        var ampm = hr >= 12 ? " PM" : " AM";
        if ( hr > 12 ) hr -= 12;
        if ( hr == 0 ) hr = 12;
        var mn = now.getMinutes();
        var sc = now.getSeconds();
        if ( mn < 10 ) mn = "0" + mn;
        if ( sc < 10 ) sc = "0" + sc;
        document.getElementById("serverTime").innerHTML = hr + ":" + mn + ":" + sc + ampm;
    }
    tick();
    setInterval( tick, 1000 );
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,188
    Thanks
    10
    Thanked 569 Times in 550 Posts
    oh what the heck, let's do a js-only version since everyone else is tossing a hat into the ring:



    Code:
    function serverTime(){
      return new Date(  
         new Date().getTime() +  
         ( new Date()  - serverTime.timeStamp  )  
      );
    }
    
    
    function sHeadDate() {
    	var XHRt = new XMLHttpRequest;
    	XHRt.open("GET", "/", false);
    	XHRt.send();
    	return new Date( XHRt.getResponseHeader("Date") );
    }
    
    serverTime.timeStamp = sHeadDate();
    
    // now, we can just call serverTime at any point to get the server's time to within a few dozen ms:
    alert(  serverTime()  );
    tested in the console on this page in chrome and firefox.
    Last edited by rnd me; 09-28-2013 at 03:30 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •