Portal Home > Knowledgebase > Articles Database > [PHP + JS] Live Server Time Display


[PHP + JS] Live Server Time Display




Posted by latheesan, 08-05-2008, 07:03 PM
Hello, I wrote this simple script to show the current server time. The code looks something like this so far: test.php When i run this script, I see the "Loading ..." message for about a second, and then the server time get shown "once". It is not updating every 1 second as you would expect it. Where im i going wrong with this? Demo : http://agito.ath.cx/test.php

Posted by Barti1987, 08-05-2008, 10:55 PM
When you insert PHP code into Javascript, on page load, that PHP will turn into HTML. View the source of your website and you will see that the function displayTime() will always display the server time on page load. You can fix it in two ways: * Adjust your function to actually increment the time. * Use Ajax to reload the time. Peace,

Posted by vibrokatana, 08-06-2008, 02:19 AM
That PHP code is only executed when the page is loaded. So it is probably working properly. As stated above, you just need to increment it (using ajax is probably overkill)

Posted by etogre, 08-06-2008, 02:38 AM
I tried to rewrite this using the javascript Date() object, and setting the date to the current server time. Unfortunately when you set Date() it doesn't increment on it's own. So you're going to have to come up with a method for doing that, or maybe find a way to get Date() to increment set values. Here's the code that didn't work, so you see what I'm talking about. Maybe it will give you a jumping off point...

Posted by latheesan, 08-06-2008, 09:55 AM
Thanks for the input, i think i know why my code didn't work now. I figured out an alternative way and its working great. You can see a demo if it here : http://agito.ath.cx/helioscp

Posted by JustinSmall, 08-06-2008, 12:38 PM
Oh that looks very nice, maybe you should post the code, I won't be using it... some people might find it helpful As much as I love Ajax, some people do overkill it. JavaScript is a very useful element on web pages. P.S. Your header is very nice.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read