My Server’s Clock

Sometimes we want to know the exact time on the server. We can get the timestamp on server using PHP simply by using the time() functions. But if we need a clock, a real living clock, we gonna need to incorporate PHP with clientside programming such as JavaScript

The idea is simple, get the server timestamp, and then we calculate with the local timestamp, and script a simple javascript clock with the server timestamp. I’m not gonna wasting more time, here’s the code:

<script type="text/javascript">
<!--
function clock() {
    document.write('<span id="clock"></span>');
    // here's the PHP code that spawn out the server local time
    <?php list($sy,$sm,$sd,$sh,$si,$ss) = explode(",", date("Y,n,j,G,i,s")); ?>
    // now we represent the server time as javascript date
    var server = new Date(<?=$sy?>, <?=$sm?>, <?=$sd?>, <?=$sh?>, <?=intval($si)?>, <?=intval($ss)?>).getTime();
    // calculate the client time
    var client = new Date().getTime();
    // run our ticker
    tick(client-server);
}
function tick(diff) {
    var d = new Date(new Date().getTime()-diff);
    var i = d.getMinutes(); if(i < 10) i='0'+i;
    var s = d.getSeconds(); if(s < 10) s='0'+s;
    var month_names = ',Januari,Februari,Maret,April,Mei,Juni,Juli,Agustus,September,Oktober,November,Desember'.split(/,/g);
    var clock = document.getElementById("clock");
    if(clock) {
        clock.innerHTML='Server: <?=$_SERVER["HTTP_HOST"]?> Time: '+
            d.getDate()+'/'+month_names[d.getMonth()]+'/'+d.getFullYear()+' '+
            d.getHours()+':'+i+':'+s;
        setTimeout('tick(' + diff + ');', 1000);
    }
}
//-->
</script>

Hope it helps, there’s an external resources available

3 komentar

  1. Aryo Sanjaya bilang:

    Fer, would you please make me some script, just like yours, but I need user may switch between local (client) and server time.
    You may take a look at MotoGP.com, that already implement it.

    Thank you.

    November 21st, 2006 at 14:44
  2. isdah junker ngalam bilang:

    weik… titlenya itu loh… so spam geto…

    November 21st, 2006 at 23:06
  3. Robert Lozyniak bilang:

    Your clock has a bug.

    You forgot about the difference between PHP month numbering and JavaScript month numbering. PHP counts months as the Japanese do: January = 1, February = 2, etc. JavaScript is different. In JavaScript, January is month 0, February is month 1, etc.

    You can take these lines
    // here’s the PHP code that spawn out the server local time

    // now we represent the server time as javascript date
    var server = new Date(, , , , , ).getTime();
    and replace them with
    // here’s the PHP code that spawn out the server local time

    // now we represent the server time as javascript date
    // note we subtract 1 from the month
    var server = new Date(, - 1, , , , ).getTime();
    or if you want UTC you can just do this
    // here’s the PHP code that spawn out the server local time

    // now we represent the server time as javascript date
    var server = ;
    Note: none of my code has been tested!

    July 11th, 2007 at 03:16

Tinggalkan Komentar





*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word