views:

23

answers:

1

Hi All,

I am building a timer for a website that will count down between two php date()s. The whole site uses php and there are a lot of time/date functions which rely on date_default_timezone_set() etc.

So, back tot he timmer...

I can get it working, well if I refresh my screen every second, the problem I currently have is the date/time is called via php, and obviously wont refresh when the javascript function does. What would be the best action? How could I get the:

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

part of my script to update?

Current code (uses mootools to flip image down like a airport display board, removed for example):

    <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>
A: 

You can't put Php in a javascript script (Php is a server side technology whereas javascript is a client side technology) but you can retrieve your dates using Ajax.

Spilarix
PHP can, however, output text that can be used as javascript. That's often easier than relying on AJAX.
Gordon
I had thought about Ajax, but was hoping for another way, don't fancy loading another file every second...
jimbo