SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: PHP Countdown

  1. #1
    SitePoint Addict buildakicker's Avatar
    Join Date
    Jun 2005
    Location
    NorCal
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up PHP Countdown

    Hello and Happy New Year to you all!

    I would like to make a simple count down script in PHP.

    Before a page refreshes I would like to have the number countdown from 5 to 0.

    I have:
    Code:
        $i=5;
        while($i!=0){
            echo $i;
            //*1000?
            $i--;    
        }
    This is fine, but how do I make it stall for 1 second before it counts to 4, then 3, then 2, then 1, then 0???

    Also, can I overwrite what is showing up on the screen, rather than have it like: 54321?

    Thanks.
    SKILEASES.COM - FREE rental listings!
    WILDFIREBLOG.COM - Wildland Fire microblog!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Not with PHP. You need JavaScript for client-side effects (stuff that happens after the HTML is sent to the browser).

  3. #3
    SitePoint Addict buildakicker's Avatar
    Join Date
    Jun 2005
    Location
    NorCal
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks!
    SKILEASES.COM - FREE rental listings!
    WILDFIREBLOG.COM - Wildland Fire microblog!

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I know this isnt the javascript forum....

    BUT

    just for completeness

    use this code

    Code:
    <script language="JavaScript">
     var sec=1 
     var mins=90 
     var dispmin=0
     var color="black"
    function leadingZero(x){
       return (x>9)?x:'0'+x;
    }
     
    function display(){ 
     if (sec<=0){ 
        sec=60 
        mins-=1
     } 
     if (mins<=-1){
        document.getElementById('counter').innerHTML="TIMED OUT"
     alert ("You have timed out your session with us, use the browsers Refresh button to continue")  
        return; 
     } 
     else 
        sec-=1
     sec=leadingZero(sec)
     dispmin=leadingZero(mins) 
     document.getElementById('counter').innerHTML="Déconnexion dans  -: "+dispmin+":"+sec 
     if (mins < 10) {document.getElementById('counter').style.backgroundColor="red" }
        setTimeout("display()",1000) 
    } 
    display()
     
    </script>
    then in your HTML locate a div with the id 'counter' positioned where you want the timer, the code above gives you 90mins, then counts down in seconds, once it reaches 00:00 it says TIMED OUT.

    Hope it helps

    Terry
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


Bookmarks

Posting Permissions

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