2010-10-18 149 views
0

我正在爲一個網站構建一個計時器,它將在兩個php日期()之間倒計時。 整個網站使用PHP和有大量的時間/日期函數依賴於date_default_timezone_set()等Javascript計時器在兩個php日期之間倒計時

所以,回到托特他蒂默...

我能得到它的工作,那麼,如果我每秒刷新我的屏幕,我現在遇到的問題是通過php調用日期/時間,並且當javascript函數執行時顯然不會刷新。最好的行動是什麼?我怎麼能得到:我的腳本

dateNow = new Date(<?php date_default_timezone_set($row_currentauction['timezone_auc']); echo date("Y,m,d,H,i,s"); ?>); 

部分更新?

當前代碼(使用mootools的翻轉圖像向下像機場的顯示牌,例如刪除):

<script type="text/javascript"> 
    //<![CDATA[ 
    function retroClock(){ 

     dateFuture = new Date(<?php echo javadateEnd($row_currentauction['startdate_auc'], $row_currentauction['duration_auc'], $row_currentauction['extend_auc'],$row_currentauction['timezone_auc']); ?>); 
     dateNow = new Date(<?php date_default_timezone_set($row_currentauction['timezone_auc']); echo date("Y,m,d,H,i,s"); ?>); 

     difference = dateFuture.getTime() - dateNow.getTime(); //calculates difference between dates in ms 

     days = 0; hours = 0; mins = 0; secs = 0; 

     difference = Math.floor(difference/1000); //omit the "milliseconds" 

     days = Math.floor(difference/86400);//days 
     difference = difference%86400; 

     hours = Math.floor(difference/3600);//hours 
     difference = difference%3600; 

     mins = Math.floor(difference/60);//minutes 
     difference = difference%60; 

     secs = Math.floor(difference);//seconds 

     if(days < 2){document.getElementById('days').innerHTML="day";}else{document.getElementById('days').innerHTML="days";} 
     if(hours < 2){document.getElementById('hours').innerHTML="hour";}else{document.getElementById('hours').innerHTML="hours";} 
     if(mins < 2){document.getElementById('minutes').innerHTML="minute";}else{document.getElementById('minutes').innerHTML="minutes";} 
     if(secs < 2){document.getElementById('seconds').innerHTML="second";}else{document.getElementById('seconds').innerHTML="seconds";} 

     //change pads 
     //rest of script... 
    } 

    setInterval('retroClock()', 1000); 

    //]]> 
</script> 

回答

0

你不能把PHP的在JavaScript腳本(PHP是一種服務器端技術,而JavaScript的是客戶端技術),但您可以使用Ajax.

+0

檢索您的日期,但是PHP可以輸出可用作javascript的文本。這通常比依靠AJAX更容易。 – GordonM 2010-10-18 16:16:26

+0

我曾經想過Ajax,但希望有另一種方法,不要幻想每秒鐘加載另一個文件... – jimbo 2010-10-19 07:32:18