Page 1 of 1

a countdown timer

Posted: Thu Oct 15, 2009 5:05 pm
by farshad_momtaz
hi,
i need a timer that count down the time from 30 to 0 and if the timer is zero the script run php code # 1 ( the timer will stop and if user refreshes the page it is still zero) and if the user hit button # 1 the timer will restart to 30 and the script will run php code # 2 ( the timer will count down again until it is zero). i want the script to work between 1 pm to 3pm(so it starts automatically i at 1)and if the timer didn't get to zero until 3pm i want it to stop counting down and show a message or goes to a different page.
and obviously the timer should show a same number for all the users!
is there any way to do that and if yes how?
can i do it with ajax and if yes how?

thank you

farshad

Re: a countdown timer

Posted: Thu Oct 15, 2009 10:40 pm
by farshad_momtaz
thanks for your answer but i didn't understand what to do!
this is the code that i wrote to declare weather the time is past the endtime or not and it finds the time left but the problem with it is it doesn't count it down it just shows the number so the user have to refresh to get the new number and even user number one presses the reset button user number 2 have to refresh the page to see that the timer was reset.

Code: Select all

endtime = (timecode of timer endtime)
current_time = time()
 
if (current_time >= endtime) {
  echo('TIMER EXPIRED');
} else {
  echo('time remaining: '.(timer endtime - current_time));
}

Re: a countdown timer

Posted: Fri Oct 16, 2009 1:21 am
by farshad_momtaz
this is an understand able version of what i just said :)
I am making a website and the page is oriented around a countdown system. If the timer ever reaches 0, the whole thing will stop. Below are the specifics:

I need a countdown timer that runs from 30 seconds to 0 seconds. If the timer is at 0 seconds, the script runs php code #1 (which makes the timer stop and if any user refreshes the page, it will still stay at zero).
There is also two buttons. If user clicks on button #1, the timer will get reset to 30 seconds remaining. Then the script will run the php code #2 (which allows the timer to begin counting down again from 30 seconds). If user clicks on button #2, the timer will get reset to 5 seconds remaining and same thing follows as the first button.

I want the timer counter script to between before certain hours (like let's say, noon-4PM). I want the counter to automatically begin at Noon with the above instructions. Also, if by 4PM, the counter never reaches 0 (meaning it never reaches php code #1), I want the timer counter to stop, disappear, and a message appear over it to link it to a new page.

And obviously the timer should show the same number (seconds remaining) for all the users on the website!

Re: a countdown timer

Posted: Fri Oct 16, 2009 8:02 am
by Eric!
The problem McInfo is pointing out to you and you've indirectly found because people have to refresh your page for it to work is that PHP is a server side language. Meaning it only runs when the browser tells the server it needs something, the server can't contact the browser (client) at random time intervals. If you didn't understand his suggestion, you're going to find these tasks very difficult.

For this you have to use Javascript because it runs on the client. However for your application you'll also want to synchronize it to your server. This is a fair bit of work for someone new at either Javascript or PHP.

Here's a simple countdown javascript
http://www.wallpaperama.com/forums/java ... -t545.html

And more details here http://www.elated.com/articles/javascri ... tinterval/

You then need to learn how to make the javascript work with your php or you could try AJAX.

For this part:
farshad_momtaz wrote:I want the timer counter script to between before certain hours (like let's say, noon-4PM). I want the counter to automatically begin at Noon with the above instructions. Also, if by 4PM, the counter never reaches 0 (meaning it never reaches php code #1), I want the timer counter to stop, disappear, and a message appear over it to link it to a new page.

And obviously the timer should show the same number (seconds remaining) for all the users on the website!
This is more complicated and involves using a database (for tracking time between php sessions), php and javascript or AJAX. Also the automatically begin at noon is a bit complicated. The user would have to browse to your page before noon and the server would not let it start, but start a javascript counter to refresh the page around noon. When it actually is noon your php side would allow the javascript to start....more or less.

Re: a countdown timer

Posted: Sat Oct 17, 2009 11:03 pm
by farshad_momtaz
ok i made a javascript code that is a simple timer and it counts down to zero but the problem is the problem with that is when i refresh the page it goes back to 30 so there isn't a point in that and when another users is in the page our timers doesn't show the same time!!
and by the way cant i use the technology that is used in chat rooms or online chats?
and i know how to use and work with data base in php and i have a little bit of experience using ajax but im learning it.

Re: a countdown timer

Posted: Sun Oct 18, 2009 6:24 pm
by Eric!
Now you have to control the timer by setting it with php and your server depending on the timing scheme you want to setup between users and the status of everyone logged in to your database.