Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Js + Php

  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Js + Php

    hey all!

    Just a few problems I've got stuck with, causing me a headache now lol, I'm sure it's something really simple.

    I was trying to create a dynamic countdown script that updates every second but uses the server time rather than the client time, so i threw some JS and PHP together to create the below:

    Code:
    <script language="JavaScript">
    	year = '2004'
    	month = '12'
    	day = '1'
    	hour = '17'
    	minute = '00'
    	format = 1
             {
             html_code = '<span id="countdown"></span>';
             
             document.write(html_code);
             
             countdown(year, month, day, hour, minute, format);                
             }
             
    function countdown(year, month, day, hour, minute, format)
             {
             Today = new Date();
             Todays_Year = <?php echo date("Y"); ?> - 2000;
             Todays_Month = <?php echo date("n"); ?> + 1;                  
             
             //Convert both today's date and the target date into miliseconds.                           
            
     			<?php
    			echo "Todays_Date = (new Date(";
    			echo date("Y").","; 
    			echo date("n").","; 
    			echo date("j").",";
    			echo date("G").",";
    			echo date("i").","; 
    			echo date("s");
    			echo ")).getTime();";
    			?>
                                   
             Target_Date = (new Date(year, month, day, hour, minute, 00)).getTime();                  
             
             //Find their difference, and convert that into seconds.                  
             Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
             
             if(Time_Left < 0)
                Time_Left = 0;
             
             switch(format)
                   {
                   case 0:
                        //The simplest way to display the time left.
                        document.all.countdown.innerHTML = Time_Left + ' seconds';
                        break;
                   case 1:
                        //More datailed.
                        days = Math.floor(Time_Left / (60 * 60 * 24));
                        Time_Left %= (60 * 60 * 24);
                        hours = Math.floor(Time_Left / (60 * 60));
                        Time_Left %= (60 * 60);
                        minutes = Math.floor(Time_Left / 60);
                        Time_Left %= 60;
                        seconds = Time_Left;
                        
                        dps = 's'; hps = 's'; mps = 's'; sps = 's';
                        //ps is short for plural suffix.
                        if(days == 1) dps ='';
                        if(hours == 1) hps ='';
                        if(minutes == 1) mps ='';
                        if(seconds == 1) sps ='';
                        
                        document.all.countdown.innerHTML = days + ' day' + dps + ' ';
                        document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
                        document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
                        document.all.countdown.innerHTML += seconds + ' second' + sps;
                        break;
                   default: 
                        document.all.countdown.innerHTML = Time_Left + ' seconds';
                   }
                   
             //Recursive call, keeps the clock ticking.
             setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
             }
    </script>
    it used to update when it used to call the time from the client clock, but when I added the PHP to make it server-side, it now doesn't refresh adn update every second like it used to but it does display the correct countdown.

    Why is this, and how can i fix it?

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    I didn't really look at your post in debt, but noticed you are using setTimeout(), which executes a function once after a specified number of miliseconds. If you want it to execute every second...you will want to use setInterval().

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, i figured my first mistake, I included the PH in the loop and because PHP doesnt reload everytime it's refreshed, it wouldn't work... right so no I have it converted into miliseconds using PHP, and then let the JS do the rest. I no the PHP works at least :P just I can't figure out why the JS doesn't display the output right.

    Example at http://gavinpearce.co.uk/dev/phpjs2.php

    Cheers gain!

    Code:
    <html>
    <head>
    </head>
    <body>
    <script language="JavaScript">
    	
    		html_code = '<span id="countdown"></span>';
    		document.write(html_code);
    
    		Todays_Date = <?php echo time()."\n";?>
    		//hou,min,sec,mon,day,yea
    		Target_Date = <?php echo mktime(17, 00, 0, 12, 1, 2004). "\n"; ?>
    		//Find their difference, and convert that into seconds.      
    		Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
    		countdown(Time_Left);                
             
    function countdown(Time_Left)
             {                
    
    		if(Time_Left < 0)
    		  Time_Left = 0;           
    
                        days = Math.floor(Time_Left / (60 * 60 * 24));
                        Time_Left %= (60 * 60 * 24);
                        hours = Math.floor(Time_Left / (60 * 60));
                        Time_Left %= (60 * 60);
                        minutes = Math.floor(Time_Left / 60);
                        Time_Left %= 60;
                        seconds = Time_Left;
                        
                        dps = 's'; hps = 's'; mps = 's'; sps = 's';
                        //ps is short for plural suffix.
                        if(days == 1) dps ='';
                        if(hours == 1) hps ='';
                        if(minutes == 1) mps ='';
                        if(seconds == 1) sps ='';
                        
                        document.all.countdown.innerHTML = days + ' day' + dps + ' ';
                        document.all.countdown.innerHTML += hours + ' hour' + hps + ' ';
                        document.all.countdown.innerHTML += minutes + ' minute' + mps + ' and ';
                        document.all.countdown.innerHTML += seconds + ' second' + sps;
    		
    		Time_Left = Time_Left-1
    
                   
             //Recursive call, keeps the clock ticking.
             setTimeout('countdown(' + Time_Left + ');', 1000);
             }
    </script>
    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •