View Full Version : AJAX -php auto refresh time

12-05-2008, 04:15 AM
hi all

i have a time.php file in which i have code

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

this code shows the time on the page.

i want the ajax code that wil auto refresh the time after every 5 second. i dont want to auto refresh the whole page.

i dont have knowledge of ajax but know that this is posible with ajax only.


12-05-2008, 05:59 AM
I'm not sure what you mean, but I decided it was that you wanted time.php imported to a page every 5 seconds. So I tried that out using my current favorite AJAX routine (http://www.dynamicdrive.com/forums/showpost.php?p=143849&postcount=16), and it worked:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function loadXmlHttp(url, id) {
var f = this;
f.xmlHttp = null;
/*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
/*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
@end @*/
if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
else if (/(object)|(function)/.test(typeof createRequest))
f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
else {
f.xmlHttp = null;
// Internet Explorer 5 to 6, includes IE 7+ when local //
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
@end @*/
if(f.xmlHttp != null){
f.el = document.getElementById(id);
f.xmlHttp.onreadystatechange = function(){f.stateChanged();};

loadXmlHttp.prototype.stateChanged=function () {
if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
this.el.innerHTML = this.xmlHttp.responseText;

var requestTime = function(){
new loadXmlHttp('time.php', 'timeDiv');
setInterval(function(){new loadXmlHttp('time.php?t=' + new Date().getTime(), 'timeDiv');}, 5000);

if (window.addEventListener)
window.addEventListener('load', requestTime, false);
else if (window.attachEvent)
window.attachEvent('onload', requestTime);
<div id="timeDiv">


12-05-2008, 07:02 AM
hi john

let me clear my self about what i want to do.

i have time.php file in the my right colum that is included file with


in time.php i have

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

which shows the time.

on first visit to the page it wil show you the present time and if u r on that page for 20 minutes and if u dont refresh the page the time will be shown that is of 20minitus back.

what i need is that the time.php file should get refresh automatically after 5 seconds so that it looks like the live clock which is showing you the present time with auto refresh server side.


12-05-2008, 07:22 AM
hi john

i had tried it and it is working very fine as i needed like a live clock.

thanks very much.