Help Please!

Discussion in 'JavaScript' started by pubsit1000, Nov 28, 2008.

  1. #1
    I want to show local time on a page based on server time. I have the php but it only shows a static clock. I need to make it update real time each second and don't know how. I believe it can be easily done in js but I have no clue.

    
    <?php
    $diff=-4;
    $newtime=date("U")-$diff*3600;
    print date("g:i:s A", $newtime);
    ?>
    
    Code (markup):
     
    pubsit1000, Nov 28, 2008 IP
  2. Soumyabrata

    Soumyabrata Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use timer function set a interval of 1000 mSec that means a second use a function to calculate mnts & hrs
     
    Soumyabrata, Dec 3, 2008 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    yes but strictly speaking, unless you use ajax--it won't be 100% accurate as per the server time. once you get the initial values and start relying on setInterval(showClock, 1000), you start relying on 2 things that tend to not be reliable: setTimeout's implementation and stability and the client's browser and cpu availability. this produces accumulative errors that over time can mount up and result in the time shown to be way different to the server one.

    of course, you can set it to use ajax to fetch the output of your PHP script but that's not ideal either. once again, it will need to rely on setTimeout although this time, you need to allow for the completion of the XHTTP request. so, if you get it to getTime every 1000ms, the response can typically be 60-200ms, hence the update will come to the browser on the onComplete/success event at around 1200ms after the previous one. once again, not ideal. what you really need is a socket connection open ...

    but it's all irrelevant if total accuracy is not important to you - so unless this will sit behind an atomic clock NTP server, any old js clock will do the trick, you just need to work out the difference between clientside time and server time at the start.
     
    dimitar christoff, Dec 5, 2008 IP