Javascript计时器在两个php日期之间倒计时

时间:2010-10-18 15:50:19

标签: php javascript date

我正在为一个网站构建一个计时器,它将在两个php date()之间倒计时。 整个站点使用php,并且有很多时间/日期函数依赖于date_default_timezone_set()等。

所以,回到他的时候......

我可以让它工作,如果我每秒刷新我的屏幕,我目前的问题是通过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>

1 个答案:

答案 0 :(得分:0)

你不能把PHP放在javascript脚本中(Php是服务器端技术,而javascript是客户端技术)但你可以使用Ajax.

检索你的日期