Countdown timer

Need a little help on how to do this, possibly with javascript.

Page 1 of 1

1 Replies - 18631 Views - Last Post: 14 October 2008 - 03:58 PM Rate Topic: -----

#1 sunshinekiller   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-October 08

Countdown timer

Posted 14 October 2008 - 03:39 PM

I am working on a simple timing game and dont know how i would go about this without refreshing the page to get it to simulate a real time timer. If anyone can help me i would really appreciate it, it can be done in javascript but if i did that i wouldnt know how to say hey when this reaches 0 do this in php...
for my countdown timer i currently have written up

header( 'refresh: 1; url=countdown.php' );

// countdown function
// parameters: (year, month, day, hour, minute)

function countdown($year, $month, $day, $hour, $minute)
  // make a unix timestamp for the given date
  $the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year, -1);

  // get current unix timestamp
  $today = time();

  $difference = $the_countdown_date - $today;
  if ($difference < 0) $difference = 0;

  $days_left = floor($difference/60/60/24);
  $hours_left = floor(($difference - $days_left*60*60*24)/60/60);
  $minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
  echo "Today's date ".date("F j, Y, g:i a")."<br/>";
  echo "Countdown date ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
  echo "Countdown ".$days_left." days ".$hours_left." hours ".$minutes_left." minutes left";

you can see my refresh but if i get over 100-2k people using it that would be stupid to do and somewhat more demanding on the server. Please help me with different solutions and ideas. Thanks for those that reply. and no this isnt for school.

This post has been edited by sunshinekiller: 14 October 2008 - 03:40 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Countdown timer

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1513
  • View blog
  • Posts: 8,289
  • Joined: 07-September 06

Re: Countdown timer

Posted 14 October 2008 - 03:58 PM

Look into Ajax, that way you can have javascript do your countdown and then send a command to the server when the timer runs out (via Ajax). As PHP is server side, you won't be able to do a live countdown.

The one thing you will have to make sure that you take into account when using Javascript and Ajax to do this stuff is, if your stuff is time sensitive, you will want to disable the form when the timer goes off (before the Ajax call is sent) this is because Ajax is not immediate with the return data, so making it so the people can't answer more questions (or whatnot) will make the results closer to reality.

Hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1