1

I'm making a site for an Informatics project of mine. For that I want to make:

  1. php script that outputs a date which is can be stored in a Database.
  2. Javascript counts down to that date. If it's finished I want to display a hyperlink (simple HTML anchor).

Item 1 has already been done, but I am having a difficult time achieving item 2. We only learned HTML, MySQL and PHP so far. So I'm learning Javascript at the moment. All the examples on this site are too difficult to understand and there must be an easier way to do it. I want to understand the code.

PHP (no edit needed):

function Klaar_Bouw($getal=0) {
      $nu = strtotime("now");
      $dag = floor($getal / 86400);
      $uur  = floor(($getal % 86400) / 3600);
      $min = floor(($getal % 3600) / 60);
      $sec = ($getal % 60);
      $nieuw = date('d-m-Y H:i:s', mktime(
      date('H',$nu)+$uur,
      date('i',$nu)+$min,
      date('s',$nu)+$sec,
      date('m',$nu),
      date('d',$nu)+$dag,
      date('Y',$nu))
      );
      return $nieuw;
              }
$bouwklaar = Klaar_Bouw( -! random number in seconds !-);
echo"$bouwklaar";

Javascript:

function Bouwen(BouwKlaar) {
    var bouwtijd = new Date(BouwKlaar);

    var dag = (getUTCDay(bouwtijd) - getUTCDay());
    var uur = (getUTCHours(bouwtijd) - getUTCHours());
    var min = (getUTCMinutes(bouwtijd) - getUTCMinutes());
    var sec = (getUTCSeconds(bouwtijd) - getUTCSeconds());

    return dag + ":" + uur + ":" + min + ":" + sec;
}

setInterval(function () {
    var bouw = Bouwen('2013, 05, 21, 20, 00, 00');
    document.getElementById("datum").innerHTML = bouw;
}, 500);

Output Format: dd:hh:mm:ss (counting down to 0, then output HTML anchor link)

1
  • Just a tip man. I do like dutch language even not knowing anything of it. But SO is world wide QA site, so people from entire world get in here, so it have to write in english. Furthermore, if you translate your code(vars, comments, whatever) to english, even just to post here, it will be nicer, cause it helps people for understand it. Commented May 21, 2013 at 17:17

2 Answers 2

2

You're misusing the Date object. Try using like this:

function Bouwen(year, month, day, hour, minute, second) {
    var bouwtijd = new Date(year, (month - 1), day, hour, minute, second);

and of course:

var bouw = Bouwen(2013, 5, 21, 20, 0, 0);

You interval doesn't need to run at the frequency of 500 miliseconds if your countdown will be refresh on each second. So use: window.setTimeout('targetFunction()', 1000) for each second. Plus, it will print the same time ever cause you're not changing it. To change it you'll have to set your function to call itself by the interval decreasing by 1 from seconds. But before the call of the interval, you'll have to compare if your current date(of the countdown) is equal another one(that you not specified on your text) to stop it and show your hyperlink.

Good luck.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey, thanks for your reaction! I used the jsFiddle form George, because it was much clearer. I appriciate your input! I've only one question left: how to use that window.setTimeout? I can't get it how to use it. Can you make a fiddle for me? For me it's much easier to interpertate a code than text.
The setInterval in George's answer works fine too. Just set it to a var like this var interval = setInterval so, when countdown finishes you just stop the interval clearInterval(interval).
Still got one problem: The countdown doesn't stop! I've got this fiddle: jsfiddle.net/Trooper_X/MB2jB . Can you get a look for me at it? I still don't get it, why it isn't working :/
Change your condition to stop interval to this if (dag == 0 && uur == 0 && min == 0 && sec == 0) {
Thank you! I really appreciate it! Everything works great now! The PHP will limit the function so it cannot countdown after the date. Great help! Thanks!
0

Take a look at this jsFiddle. Several issues:

  • You are using the new Date function incorrectly. This function receives several integer inputs, not a formatted string.
  • Notice how the days, minutes, hours, and seconds are calculated. You should subtract the dates FIRST, then retrieve results from the getUTC functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.