php结合js动态获取空间时间

 1 <h5 endtime="11/12/2012 16:00:00"></h5>
 2 <input type="hidden" value="" id="servertime"/>
 3 <script type="text/javascript" language="javascript">
 4 window.onload=function (){
 5 stime();
 6 }
 7 var c=0;
 8 <? date_default_timezone_set( "Asia/Shanghai" );?>
 9 var Y=<?php echo date('Y')?>,M=<?php echo date('m')?>,D=<?php echo date('d')?>;
10 function stime() {
11 c++
12 sec=<?php echo time()-strtotime(date("Y-m-d"))?>+c;
13 H=Math.floor(sec/3600)%24
14 I=Math.floor(sec/60)%60
15 S=sec%60
16 if(S<10) S='0'+S;
17 if(I<10) I='0'+I;
18 if(H<10) H='0'+H;
19 if (H=='00' & I=='00' & S=='00') D=D+1; //日进位
20 if (M==2) { //判断是否为二月份******
21 if (Y%4==0 && !Y%100==0 || Y%400==0) { //是闰年(二月有28天)
22 if (D==30){M+=1;D=1;} //月份进位
23 }
24 else { //非闰年(二月有29天)
25 if (D==29){M+=1;D=1;} //月份进位
26 }
27 }
28 else { //不是二月份的月份******
29 if (M==4 || M==6 || M==9 || M==11) { //小月(30天)
30 if (D==31) {M+=1;D=1;} //月份进位
31 }
32 else { //大月(31天)
33 if (D==32){M+=1;D=1;} //月份进位
34 }
35 }
36 if (M==13) {Y+=1;M=1;} //年份进位
37 //setInterval(stime,1000);
38 
39 setTimeout("stime()", 1000);
40 document.getElementById("servertime").value =new Date(Y+'/'+M+'/'+D+' '+H+':'+I+':'+S).getTime()
41 }
42 </script>
43 <script>
44   function lxfEndtime(){
45           $("h5").each(function(){
46                 var lxfday=$(this).attr("lxfday");//用来判断是否显示天数的变量
47                 var endtime = new Date($(this).attr("endtime")).getTime();//取结束日期(毫秒值)                
48                var nowtime = document.getElementById("servertime").value;        //今天的日期(毫秒值)
49                 var youtime = endtime-nowtime;//还有多久(毫秒值)
50                 var seconds = youtime/1000;
51                 var minutes = Math.floor(seconds/60);
52                 var hours = Math.floor(minutes/60);
53                 var days = Math.floor(hours/24);
54                 var CDay= days ;
55                 var CHour= hours % 24;
56                 var CMinute= minutes % 60;
57                 var CSecond= Math.floor(seconds%60);//"%"是取余运算,可以理解为60进一后取余数,然后只要余数。
58             
59 if(String(CHour).length==1){var CHour="0"+ hours % 24;}
60 if(String(CMinute).length==1){CMinute="0"+ minutes % 60;}
61 if(String(CSecond).length==1){CSecond="0"+ Math.floor(seconds%60);}
62                 if(endtime<=nowtime){
63                         $(this).html("已结束")//如果结束日期小于当前日期就提示过期啦
64                         }else{                            
65                         $(this).html("<i>剩余时间"+CDay+"天</i><em>"+CHour+" :</em><em>"+CMinute+" :</em><em class='end'>"+CSecond+" </em>");                                                    }
66           });
67    setTimeout("lxfEndtime()",1000);
68   };
69 $(function(){
70       lxfEndtime();
71    });
72 </script>


不加:<? date_default_timezone_set( "Asia/Shanghai" );?> 则为获取本地动态时间

时间戳的显示:替换43行,document.getElementById("servertime").innerHTML =new Date(Y+'/'+M+'/'+D+' '+H+':'+I+':'+S).getTime()

 

注意:

new Date('2012/11/07 15:10:43').getTime();  这里不要用"-"代替"/"  否则会有些浏览器出现NaN错误。

原始参考:http://www.codefans.net/jscss/code/2893.shtml

posted @ 2012-11-07 11:11  tinyphp  Views(370)  Comments(0Edit  收藏  举报