Current location: Hot Scripts Forums » Programming Languages » PHP » making a countdown timer

making a countdown timer

Reply
  #1 (permalink)  
Old 11-10-09, 08:28 PM
dariuc dariuc is offline
New Member
 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up making a countdown timer

I've tried to think of a few possible ways to accomplish what i need to do. so i'll outline what im aiming for really quickly.

I'd like to have a Timer display on the page

for example
1:25
and five seconds later it would read
1:20

my plan for doing this is using a bit of ajax along with my PHP.

my question is , is there any other way to do this?

also i was toying around with writing some self refreshing code..

aka. instead of it counting down by 1 second increments every 5 secs it would redisplay.

how would i go about doing that?

I get the concept of date() but i'm at a loss to using it to do what i want so any help would be appreciated. thanks!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-11-09, 07:42 AM
wirehopper's Avatar
wirehopper wirehopper is online now
Community Liaison
 
Join Date: Feb 2006
Posts: 2,338
Thanks: 17
Thanked 92 Times in 90 Posts
I'd recommend just using javascript.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-12-09, 05:49 PM
pyr0t3chnician pyr0t3chnician is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 26
Thanks: 0
Thanked 1 Time in 1 Post
AJAX would work, but its more of a pain to do it that way. I agree, just use javascript.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-16-09, 08:03 AM
therocket954's Avatar
therocket954 therocket954 is offline
Community Liaison
 
Join Date: Jul 2007
Location: Michigan, USA
Posts: 335
Thanks: 2
Thanked 8 Times in 8 Posts
Here's a basic Javascript countdown. You can use CSS to style the input box if you'd like.

HTML Code:
<form name="countdown">
<input type="text" size="8" name="timer">
</form> 

<script> 
<!-- 
// 
 var milisec=0 
 var seconds=30 
 document.counter.timer.value='30' 

function display(){ 
 if (milisec<=0){ 
    milisec=9 
    seconds-=1 
 } 
 if (seconds<=-1){ 
    milisec=0 
    seconds+=1 
 } 
 else 
    milisec-=1 
    document.counter.timer.value=seconds+"."+milisec 
    setTimeout("display()",100) 
} 
display() 
--> 
</script> 
__________________
--Eric Allison
Twitter: http://www.twitter.com/Eric_Allison
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-23-09, 02:08 PM
ruben_innerdev ruben_innerdev is offline
New Member
 
Join Date: Nov 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 11-23-09, 06:24 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,029
Thanks: 14
Thanked 34 Times in 33 Posts
Search this forum for a post by "Job0107" with the keyword "timer"...he posted a very good script that (I think) does what you want.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 11-24-09, 12:16 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,726
Thanks: 0
Thanked 32 Times in 32 Posts
End User is right.
I went and found that old timer script and modified it a little to be more flexible.

There are 4 variables that can be adjusted.

In the PHP section you have:
$hours
$minutes
$seconds

And in the Javascript section you have:
seconds_delay

I added in the hours and I added in the ability to delay the clocks output by a user defined interval (seconds_delay).
I also added in a feature that will automatically adjust the clock to equal increments of seconds_delay.

I have seconds_delay preset to 5 seconds.

Enter the number of hours, minutes and seconds you want the timer to run, into the PHP variables.
And enter the number of seconds you want the clock to delay before changing the time remaining, into the Javascript variable.

This program uses a session variable to store the time the clock will end.
PHP Code:
<?php
session_start
();

/* If you refresh the page
   or
   leave the page to browse and come back
   then the timer will continue to count down until finished. */

// $hours, $minutes and $seconds are added together to get total time.
$hours 0;  // Enter hours
$minutes 0;  // Enter minutes
$seconds 30;  // Enter seconds
$time_limit = ($hours 60 60) + ($minutes 60) + $seconds// Convert total time into seconds
if(empty($_SESSION["end_time"])){$_SESSION["end_time"] = mktime(date(G),date(i),date(s),date(m),date(d),date(Y)) + $time_limit;} // Add $time_limit (total time) to start time. And store into session variable.
?>
<html>
<head>
<style>
#clock {
border:2px solid red;
font-family:verdana;
font-size:16pt;
font-weight:bold;
background: #FECFC7;
width:125px;
text-align:center;
color:#000000;
}
</style>
<script>
var seconds_delay = 5; // Adjust this value for number of seconds to delay before changing the time display.
var ct,mins,secs,hrs,sw = 1;
function calculate_time()
{
 var end_time = "<?php echo $_SESSION["end_time"]; ?>"; // Get end time from session variable (total time in seconds).
 var dt = new Date(); // Create date object.
 var time_stamp = dt.getTime()/1000; // Get current minutes (converted to seconds).
 var total_time = end_time - Math.floor(time_stamp); // Subtract current seconds from total seconds to get seconds remaining.
 hrs = Math.floor(total_time / 60 / 60);
 mins = Math.floor((total_time / 60) - (hrs * 60)); // Extract minutes from seconds remaining.
 secs = total_time - ((mins * 60) + (hrs * 60 * 60)); // Extract remainder seconds if any.
 if(secs < 10){secs = "0" + secs;} // Check if seconds are less than 10 and add a 0 in front.
 if(mins < 10){mins = "0" + mins;} // Check if minutes are less than 10 and add a 0 in front.
 if(hrs < 10 && hrs > -1){hrs = "0" + hrs;} // Check if hourss are less than 10 and add a 0 in front.
 document.getElementById("clock").value = hrs + ":" + mins + ":" + secs; // Display remaining minutes and seconds.
 
 // This if statement adjusts time to increments of seconds_delay
 if(secs % seconds_delay > 0)
 {
  sw = 0;
  ct = setTimeout("calculate_time()",500);
  }
 else{sw = 1;}
 // Check for end of time, stop clock and display message.
 if(hrs < 0 && mins >= 0 && secs >= 0 || hrs == 0 && mins == 0 && secs == 0)
 {
  clearTimeout(ct);
  document.getElementById("clock").value = "00:00:00";
  alert("The time is up.");
  sw = 0;
  }
 if(sw){ct = setTimeout("calculate_time()",seconds_delay * 1000);}  // Display the time once every seconds_increment seconds. 1000 equals approximatly 1 second.
 }
</script>
</head>
<body onload="ct = setTimeout('calculate_time()',1);">  <!-- // Start clock. // -->
<input id="clock" readonly>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 11-24-09 at 12:23 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 12-04-09, 03:37 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
I have a couple of questions regarding using this timer:

1. I've noticed that if a user was to clear their browser sessions the timer seems to reset itself since that is were it's being stored. Is it possible to combine this script with something like AJAX to store the timer so it won't reset if a user clears their session?

2. Would it be possible to have multiple timers that are independent of one another, just as an example six of them, with this code or would more code be needed?

Something like <input id="clock" readonly> and <input id="clock_2" readonly> etc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 12-04-09, 06:48 PM
wirehopper's Avatar
wirehopper wirehopper is online now
Community Liaison
 
Join Date: Feb 2006
Posts: 2,338
Thanks: 17
Thanked 92 Times in 90 Posts
You could use a session timer. I used a 'ping', where javascript pings the server every minute to see if the session expired.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 12-10-09, 07:40 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,726
Thanks: 0
Thanked 32 Times in 32 Posts
Quote:
Originally Posted by PopSmith View Post
I have a couple of questions regarding using this timer:

1. I've noticed that if a user was to clear their browser sessions the timer seems to reset itself since that is were it's being stored. Is it possible to combine this script with something like AJAX to store the timer so it won't reset if a user clears their session?
I suppose you could use a file to store the end time instead of a session variable.
Quote:
Originally Posted by PopSmith View Post
2. Would it be possible to have multiple timers that are independent of one another, just as an example six of them, with this code or would more code be needed?

Something like <input id="clock" readonly> and <input id="clock_2" readonly> etc.
Yes it would be possible to have more than one timer going at once.
But it would require additional programming.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Timer / Countdown Timer davesd C/C++ 1 08-29-07 06:38 AM
countdown timer for a test. 30mins. audi PHP 5 07-26-07 03:22 AM
Self restarting countdown timer wektis Script Requests 0 10-27-06 06:31 PM
Countdown Timer Makien Script Requests 2 10-24-05 01:53 PM
Countdown Timer Makien JavaScript 2 10-06-05 04:49 PM


All times are GMT -5. The time now is 09:17 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.