Click to See Complete Forum and Search --> : How to display server time on the browser


mithun
January 29th, 2005, 12:12 AM
Hi freind I am trying to insert clock in my web page what i want is that the time in the clock should match with my server time not the time of the client's pc
can any one help me by sending some code its urgent

Dr. Script
January 29th, 2005, 07:17 AM
If you have php, make it a .php extension, than add:<?php

echo (date("H:i:s"));

?>wherever you want the sever time. With JavaScript, it is impossible, since JavaScript can't run from the server (although there is a JavaScript server side language, though I'm unfamilar with it).

Dr. Script

mithun
January 31st, 2005, 02:48 AM
If you have php, make it a .php extension, than add:<?php

echo (date("H:i:s"));

?>wherever you want the sever time. With JavaScript, it is impossible, since JavaScript can't run from the server (although there is a JavaScript server side language, though I'm unfamilar with it).

Dr. Script
This will give you only static time i want to display clock like stuff

Dr. Script
January 31st, 2005, 04:15 PM
Try something like:<!-- DocType -->
<html>
<head>
<title>Server Time</title>
<script type="text/javascript">

Number.prototype.toHours = function() {
return (this > 12) ? this-12 : this;
}

Number.prototype.pad = function() {
return (this < 10) ? "0"+this : this;
}

Date.prototype.toTimeString = function() {
return [this.getHours().toHours().pad(),this.getMinutes().pad(),this.getSeconds().pad()].join(':');
}

var tm = "<?php print (date("h:i:s")); ?>";
tm = tm.split(':');

var t = new Date();
t.setHours(tm[0]);
t.setMinutes(tm[1]);
t.setSeconds(tm[2]);
onload = setInterval(showTm,1000);

function showTm() {
t.setTime(t.getTime()+1000);
document.getElementById('tm').firstChild.nodeValue = 'Time: '+t.toTimeString();
}

</script>
</head>
<body>
<span id="tm">Time</span>
</body>
</html>This of course will have to be a php file on a server with php enabled. Otherwise, you could use other languages replacing the actual <?php ?> code in the script. That should work for you.

Dr. Script