Page 1 of 1

PHP Countdown Timer

Posted: Thu Mar 25, 2010 3:14 am
by barrowvian
I've been having a quick look around the internet and have found a few of these, but I'm looking for something a little more specific;

I'm wanting a countdown timer that will let me count down 24 hours, once the timer reaches 0 then the current page (where the countdown timer is located) will redirect to a new page when someone tries to view it, and/or will refresh and redirect if someone is still active on the page once the countdown has finished.

Is this possible to make in PHP or would a different language be more efficient?

Thanks

Re: PHP Countdown Timer

Posted: Thu Mar 25, 2010 4:40 am
by Grizzzzzzzzzz
you'd be better off in javascript

Re: PHP Countdown Timer

Posted: Thu Mar 25, 2010 5:34 am
by barrowvian
Yea a few people I have spoken to have said javascript would be the best route for the timer, however, I'm lead to believe that with javascript being client side that it will run off of the users own clock and that it will restart from scratch when the page is reloaded? If this is the case then it isn't the correct option for this type of timer.

Would there be any way to make a timer using php so that the clock will run continuously (even if the page is not being viewed)? And then when it reaches zero incorporate another function to take over and do the redirect?

Re: PHP Countdown Timer

Posted: Thu Mar 25, 2010 6:55 am
by dejvos
I am afraid there is not such a functionality. PHP is server side thats mean: When page is generated PHP loses control of the page. You have to use some client side solution. If you are afraid of the time of the client computer you would wrote a Javascript function (Java applet, ... ) which would remember a time when the page was generated and ask server about current time. Than you can substract current time and the time when page was generated ... and thats it ... pretty complicated solution but I think it's only think you can do.

Re: PHP Countdown Timer

Posted: Thu Mar 25, 2010 11:40 am
by flying_circus
barrowvian wrote:I've been having a quick look around the internet and have found a few of these, but I'm looking for something a little more specific;

I'm wanting a countdown timer that will let me count down 24 hours, once the timer reaches 0 then the current page (where the countdown timer is located) will redirect to a new page when someone tries to view it, and/or will refresh and redirect if someone is still active on the page once the countdown has finished.

Is this possible to make in PHP or would a different language be more efficient?

Thanks
You could create and store a timestamp for the start of your 24 hour period. On every page request, your script would compare the current time to the stored timestamp. If 24 hours have elapsed, then it can redirect that user to a different page.

As far as forcing a page to refresh after 24 hours, there is no reliable way to do this.

Re: PHP Countdown Timer

Posted: Thu Mar 25, 2010 12:10 pm
by Grizzzzzzzzzz
As far as forcing a page to refresh after 24 hours, there is no reliable way to do this.
Simple as sin, but couldn't you grab the remaining number of seconds in a PHP variable and simply output it on the page

Code: Select all

 
print "<meta http-equiv=\"refresh\" content=\"".$remainingSeconds."\">";
 

Re: PHP Countdown Timer

Posted: Thu Mar 25, 2010 12:18 pm
by flying_circus
As far as I understand it, Meta Refresh has been deprecated for a while now.