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