[REQUEST]Count down time

Skilled Illusionist
Joined
Apr 13, 2011
Messages
386
Reaction score
69
Can anyone please help me with an countdown time that can be seted to start at time and end at another time two time a day?
Regards.
 
PHP:
<html>
  <head>
    <script type="text/javascript">
      var num = number of seconds;
      var timer;
      function countdown() {
        num--;
        document.getElementById('countdown_ele').innerHTML = num;
        if(num < 1){
          clearTimeout(timer);
        }
      }
      function init() {
        document.getElementById('countdown_ele').innerHTML = num;
        timer = setInterval("countdown()",1000);
      }
    </script>
  </head>
  <body onload="init();">
    <span id="countdown_ele"></span>
  </body>
</html>

Cache the time, or use a session.
 
PHP:
<? /*
   declare target date; source: http://us.imdb.com/ReleaseDates?0121766 ; 
  */
  $day   = 31;     // Day of the countdown
  $month = 12;      // Month of the countdown
  $year  = 2012;   // Year of the countdown
  $hour  = 23;     // Hour of the day (east coast time)
  $event = "New Year's Eve, 2012"; //event

  $calculation = ((mktime ($hour,0,0,$month,$day,$year) - time(void))/3600);
  $hours = (int)$calculation;
  $days  = (int)($hours/24);
/*
  mktime() http://www.php.net/manual/en/function.mktime.php
  time()   http://www.php.net/manual/en/function.time.php
  (int)    http://www.php.net/manual/en/language.types.integer.php
*/
?>
<ul>
<li>The date is <?=(date ("l, jS \of F Y g:i:s A"));?>.</li>
<li>It is <?=$days?> days until <?=$event?>.</li>
<li>It is <?=$hours?> hours until <?=$event?>.</li>
</ul>

feel free to modify for your needs
 
PHP:
<? /*
   declare target date; source: http://us.imdb.com/ReleaseDates?0121766 ; 
  */
  $day   = 31;     // Day of the countdown
  $month = 12;      // Month of the countdown
  $year  = 2012;   // Year of the countdown
  $hour  = 23;     // Hour of the day (east coast time)
  $event = "New Year's Eve, 2012"; //event

  $calculation = ((mktime ($hour,0,0,$month,$day,$year) - time(void))/3600);
  $hours = (int)$calculation;
  $days  = (int)($hours/24);
/*
  mktime() http://www.php.net/manual/en/function.mktime.php
  time()   http://www.php.net/manual/en/function.time.php
  (int)    http://www.php.net/manual/en/language.types.integer.php
*/
?>
<ul>
<li>The date is <?=(date ("l, jS \of F Y g:i:s A"));?>.</li>
<li>It is <?=$days?> days until <?=$event?>.</li>
<li>It is <?=$hours?> hours until <?=$event?>.</li>
</ul>

feel free to modify for your needs

:S I hope everyone knows you used a server-side language when a server isn't needed, short-tags and other bad practices. Keep practicing. The eye-catcher was that you mixed PHP with markups. Total reveal of worst practice right there.

You definitely want this to be done in JavaScript.


Jayden's example shall do.. just never put it with another JavaScript file since it's all in window.

Jayden, you should really run that code through JSLint. You get A LOT of errors:
(Don't sweat it too much, though, I accidentally put lint in my js all the time... just fix them all before u put ur scripts online!)
Code:
Error:

Problem at line 1 character 11: 'number' was used before it was defined.

var num = number of seconds;

Problem at line 1 character 17: Expected ';' and instead saw 'of'.

var num = number of seconds;

Problem at line 1 character 18: Expected 'of' at column 1, not column 18.

var num = number of seconds;

Problem at line 1 character 18: 'of' was used before it was defined.

var num = number of seconds;

Problem at line 1 character 18: Expected an assignment or function call and instead saw an expression.

var num = number of seconds;

Problem at line 1 character 20: Expected ';' and instead saw 'seconds'.

var num = number of seconds;

Problem at line 1 character 21: Expected 'seconds' at column 1, not column 21.

var num = number of seconds;

Problem at line 1 character 21: 'seconds' was used before it was defined.

var num = number of seconds;

Problem at line 1 character 21: Expected an assignment or function call and instead saw an expression.

var num = number of seconds;

Problem at line 2 character 7: Expected 'var' at column 1, not column 7.

var timer;

Problem at line 3 character 7: Expected 'function' at column 1, not column 7.

function countdown() {

Problem at line 4 character 9: Missing 'use strict' statement.

num--;

Problem at line 4 character 9: Expected 'num' at column 5, not column 9.

num--;

Problem at line 4 character 12: Unexpected '--'.

num--;

Problem at line 5 character 9: Expected 'document' at column 5, not column 9.

document.getElementById('countdown_ele').innerHTML = num;

Problem at line 5 character 9: 'document' was used before it was defined.

document.getElementById('countdown_ele').innerHTML = num;

Problem at line 6 character 9: Expected 'if' at column 5, not column 9.

if(num < 1){

Problem at line 6 character 11: Expected exactly one space between 'if' and '('.

if(num < 1){

Problem at line 6 character 20: Expected exactly one space between ')' and '{'.

if(num < 1){

Problem at line 6 character 20: Missing space between ')' and '{'.

if(num < 1){

Problem at line 7 character 11: Expected 'clearTimeout' at column 9, not column 11.

clearTimeout(timer);

Problem at line 7 character 11: 'clearTimeout' was used before it was defined.

clearTimeout(timer);

Problem at line 8 character 9: Expected '}' at column 5, not column 9.

}

Problem at line 9 character 7: Expected '}' at column 1, not column 7.

}

Problem at line 10 character 7: Expected 'function' at column 1, not column 7.

function init() {

Problem at line 11 character 9: Missing 'use strict' statement.

document.getElementById('countdown_ele').innerHTML = num;

Problem at line 11 character 9: Expected 'document' at column 5, not column 9.

document.getElementById('countdown_ele').innerHTML = num;

Problem at line 11 character 9: 'document' was used before it was defined.

document.getElementById('countdown_ele').innerHTML = num;

Problem at line 12 character 9: Expected 'timer' at column 5, not column 9.

timer = setInterval("countdown()",1000);

Problem at line 12 character 17: 'setInterval' was used before it was defined.

timer = setInterval("countdown()",1000);

Problem at line 12 character 43: Missing space between ',' and '1000'.

timer = setInterval("countdown()",1000);

Problem at line 12 character 17: Implied eval is evil. Pass a function instead of a string.

timer = setInterval("countdown()",1000);

Problem at line 13 character 7: Expected '}' at column 1, not column 7.

}

Undefined variable: document 3 countdown, clearTimeout 3 countdown, document 10 init, setInterval 10 init
Code:
Global countdown, init, num, number, of, seconds, timer

3 countdown()
Global num, timer

10 init()
Global num, timer

/*properties
    getElementById, innerHTML
*/

Your scripts should end up with 1 or 0 global references. Usually 0 unless you make a library.
 
DST support, also setTimeout is not a high-precision timer, so don't use it that way.

In JS:

Code:
var now = new Date(), to = <some Date>;

if (to - now >= 0) {
  // it is now the desired date or later
} else {
  // it is not
}

Guess I over-thought it.
Simple script.
 
PHP:
if ($current_time <= $max_time && $current_time >= $min_time) {
    //if between the min and max times
} else {
    // not in between min and max times.
}
 
One last question.
How do i define current time and max and min time.

PHP:
$max_time = strtotime('tomorrow');
$current_time = strtotime('today');
$min_time = strtotime('yesterday');

strtotime is the lazy man's way to make times, and it usually works. For example, a timestamp out of MySQL using DATETIME or TIMESTAMP data-types will work in strtotime with high probability of reliability.

another one
PHP:
$max_time = strtotime('-1 hour'); // 1 hour ago
$current_time = strtotime('2 hours ago'); // 2 hours ago
$min_time = strtotime('180 minutes ago'); // 180m / 60m = 3h ago
 
Back