Print This Page

Servertime Clock

Okay this isn't really totally php but, php is what gives this javascript snippet it's zing:
It echoes in the php microtime function and then subtracts the difference from the local browser's js time function to make what I personally thought impossible: A realtime ticking server clock!!!  It even works if you hibernate your computer and then boot again.

timecount.php  v .1  (debug example)  by Michael Hazzard   this version has unnecessary out puts for debug info

<?php
//echo time();//outputs php seconds since epoch event
$servertime = time() * 1000;  //change it to milliseconds from php
?>
<script type="text/javascript">
function show(difference) {
//============================== client
var js_time = new Date()
var client_time = js_time;
var js_time = js_time.getTime();

var servertime = "<?php echo $servertime; ?>";

if (difference == 0) {

var difference_time = js_time - servertime ;
}
else {
var difference_time = difference ;
}

 var php_time = js_time - difference ;

var Digital=new Date();

Digital.setTime(php_time);

var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()

if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
document.getElementById('time').innerHTML= "<br><br>var digital based on php_time var" + Digital;
//document.getElementById('time').innerHTML= "<br>" + hours+":"+minutes+":"+seconds;

document.getElementById('client_time').innerHTML= "<br><br>clienttime" + client_time;

document.getElementById('out').innerHTML = "<br><br>Servertime:" + servertime + "<br> Javascript : " + js_time +"<br>Difference time:" + difference_time;

 setTimeout("show('"+difference_time +"')", 1000); // works!!!!!
//setTimeout("updateClock('"+id+"')",1000,"JavaScript") //http://www.devx.com/tips/Tip/13646
}

</script>

</head>

<body onload="show('0')" >
<?php
echo "php date". date('l dS \of F Y h:i:s A')."<br>";
echo "php servertime".$servertime;
echo "<br>php microttime()".microtime(TRUE);
?>
<span id="time">test</span>
<span id="out">out </span>

<span id="client_time">out </span>

Live debug example here and live live example here:

Add your comments or bug fixes below.

Add A Comment

Add A Comment

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
Code in the picture:
Your Name(*):
Comment(*):
 



Previous page: Php Tutorials and Snippets
Next page: Ftp Spread