Click to See Complete Forum and Search --> : Getting server date/time
salex110
09-25-2003, 06:46 AM
Please help! I'm trying to get the date/time from the web server, not the client machine, and display it on a web page - is it possible?? I need to display it down to the seconds have it constantly counting, like a clock. Also - this web server does not suppose SSI or ASP - is there another way - Perl?? Javascript??
Thanks!
It will have to be a mix between a server side language and a client side language. What you'll want to do is feed JavaScript the current time, and have that keep it updated. Something like this should do it in PHP (Note that it could be made simplar, if you don't want to format the date as I have):
<script type="text/javascript">
servdate = new Date(<?PHP echo time()*1000; ?>);
servtime = servdate.getTime();
function showTime() {
date = new Date(servtime);
day = date.getDate();
month = (date.getMonth() < 10) ? "0"+(Number(date.getMonth())+1) : (Number(date.getMonth())+1);
year = date.getYear();
hour = (date.getHours() < 10) ? "0"+(Number(date.getHours())) : (Number(date.getHours()));
minute = (date.getMinutes() < 10) ? "0"+(Number(date.getMinutes())) : (Number(date.getMinutes()));
second = (date.getSeconds() < 10) ? "0"+(Number(date.getSeconds())) : (Number(date.getSeconds()));
document.getElementById("time").innerHTML = month+"-"+day+"-"+year+" at "+hour+":"+minute+":"+second;
servtime += 1000;
}
setInterval("showTime()", 1000);
</script>
</head>
<body onload="showTime();">
<span id="time"></span>
salex110
09-25-2003, 08:20 AM
I apologize for my lack on knowledge of PHP scripting - do I need to make any modifications to this script?? I am getting the error "Object expected" on this line:
<body onload="showTime();">
Thanks for the assistance!!:)
Does your server support PHP, and did you name the file with a .php extention?
salex110
09-25-2003, 08:27 AM
Thanks - but the web server must not be configured for php - I am trying to get SSI enabled and I will see if PHP is supported on it too.
Thanks again!
You bet... Note that the script will work with any server side language that you can return the time in milliseconds (or seconds) in... PHP return the time in seconds, so I multiplied it by 1000 to get it to milliseconds.