欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

php Ajax 局部刷新

程序员文章站 2022-06-17 11:55:04
...

php Ajax 局部刷新:

HTML部分

</head>    
<body>    
<h1>Ajax动态显示时间</h1>    
<input type="button" value="开始显示时间" id="go" οnclick="start()" />    
<p>当前时间:<font color="red"><span id="showtime"></span></font></p>    
</body>    
  
<script type="text/javascript">  
var xmlHttp;  
function createXMLHttpRequest(){  
 if(window.ActiveXObject){  
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
 }  
 else if(window.XMLHttpRequest){  
  xmlHttp = new XMLHttpRequest();  
 }  
}  
function start(){  
 createXMLHttpRequest();  
 var url="getTime.php";  
 xmlHttp.open("GET",url,true);  
 xmlHttp.onreadystatechange = callback;  
 xmlHttp.send(null);  
}  
function callback(){  
 if(xmlHttp.readyState == 4){  
  if(xmlHttp.status == 200){  
   document.getElementById("showtime").innerHTML = xmlHttp.responseText;  
   setTimeout("start()",1000);  
  }  
 }  
}  
</script>  
</html>   

PHP部分

<?php  
header("cache-control:no-cache,must-revalidate");    
header("Content-Type:text/html;charset=utf-8");  
$time = "2012-1-20 18:00:00";  
$dt_element=explode(" ",$time);  
$date_element=explode("-",$dt_element[0]);  
$time_element=explode(":",$dt_element[1]);  
$date = mktime($time_element[0],$time_element[1],$time_element[2],$date_element[1],$date_element[2],$date_element[0]);  
$nowTime = time();    
$showtime = date("北京时间Y年m月d日H:i:s",$date-$nowTime);    
if($showtime<="北京时间1970年01月01日08:00:00"){  
    echo "happy new year";  
}  
echo $showtime;