Current time: 07-30-2010, 07:40 AM Hello There, Guest! (LoginRegister)
Quick Login:


Poll: rate it up !
5
4
3
2
1
[Show Results]
 
Post Reply  Post Thread 
[php] Countdown Timer
Author Message
05-02-2006, 09:08 AM
Post: #1
[php] Countdown Timer
Get to know how many days left until a specific date. I'm quite certain you've seen some sites have these things before. Want to have something like it as well? Then read on...

Step 1: The "Basic" Code

Here's what you need to put in your PHP file:
PHP Code:
//A: RECORDS TODAY'S Date And Time
$today time();

//B: RECORDS Date And Time OF YOUR EVENT
$event mktime(0,0,0,12,25,2006);

//C: COMPUTES THE DAYS UNTIL THE EVENT.
$countdown round(($event $today)/86400);

//D: DISPLAYS COUNTDOWN UNTIL EVENT
echo "$countown days until Christmas";

?>
Parts A and D of the code are pretty much easy to understand so there is no more need for me to explain it further.

In part B of the code, if you haven't figured it out yet, the event we're going to count down to is Christmas. The numbers inside the parenthesis refer to these in order: hours (24-hour format), minutes, seconds, month, day and year. So, in the example, the event is 12AM on December 25, 2006.

Part C of the code, as the comment says, computes how many days there are until the event. I will start the explanation with the ones inside the parenthesis:

($event - $today)

Counts how many seconds there are until the event we're counting down to.

($event - $today)/86400

As you all know, 86400 seconds = 1 day. This will convert the seconds to days.

round(($event - $today)/86400)

Rounds off the days so there won't be any decimal point.

That's it for Part C of the code. It is just a simple mathematical equation combined with PHP functions.



Step 2: The "Intermediate" Code



The code above is easy and all but you'll notice that if the event is today or if it already passed, it'll display either 0 or a negative number. Plus, it always displays "days". What if it's only a day away? That would be grammatically incorrect, would it?

In this "intermediate" code, we're going to remove the need for you to worry about all those things I mentioned. Excited to learn how? Of course you are, otherwise, you wouldn't read this far. Anyways, here it is:

PHP Code:
<?php

//A
$today time();

//B
$event mktime(0,0,0,12,25,date("Y"));

//C
$apart $event $today;

//D
if ($apart >= -86400)
{
  
$myevent $event;
}

//E
else
{
  
$myevent mktime(0,0,0,12,25,date("Y")+1);
}

//F
$countdown round(($myevent $today)/86400);

//G
if ($countdown 1)
{
  echo 
"$countdown days until Christmas";
}

//H
elseif (($myevent-$today) <= && ($myevent-$today) >= -86400)
{
  echo 
"Today is Christmas!";
}

//I
else
{
  echo 
"$countdown day until Christmas";
}

?>

You'll recognize Part A in the "basic" code so there is no more need for me to explain that again.

A change was made to Part B, particularly in the year. It changed 2006 from the "basic" code to date("Y") (read Date And Time tutorial). That alteration in the code gets the current year. This is so that you won't have to update the code every year.

In Part C, it computes how far apart the event and today is. We'll need it's value for Part D.

Part D of the code checks if $apart from Part C is greater than -86400. Why is it negative when there is no negative value in 86400 seconds = 1 day? To explain, let's assume that today is the day of the event we're counting down to and it's 10PM. Remember we're counting down to 12AM and obviously, it already passed since it's 10PM already. Since the time today is greater than the time of the event, $apart will have a negative value. Now you got why we have -86400 in the code?

Yes? So, in Part D, if the event is today or before, we'll rename the variable $event to $myevent. Changing the variable name is necessary for Part F of the code.

What if the event is over and you'll have to wait until next year? Previously, you have to change the code every year. How can you remove that tedious task? This is where Part E of the code comes in. It'll automatically add a year to the event (which we now call $myevent) if it already passed.

Recognize Part F? You should. It was previously Part C of the "basic" code. The only difference is the variable $myevent. You already know how this part works so moving on...

In Parts G, H and I are for displaying the countdown and their correct grammar. Just do the math, you'll figure out why they're like that.



Conclusion
Pretty nifty, huh? "Basic" or "Intermediate" code? Choose your poison.<?php

code, proud member of GFX-Depot since Feb 2006.
[Image: coooooooddddeeeee8dx.jpg]
Find all posts by this user
Quote this message in a reply
05-11-2006, 06:45 PM
Post: #2
RE: [php] Countdown Timer
nice one man, thanks for this Laugh

Find all posts by this user
Quote this message in a reply
07-19-2009, 12:20 PM
Post: #3
RE: [php] Countdown Timer
To display your count down you should better use javascript to keep updating it, like here http://freecountdown.co.cc/
Find all posts by this user
Quote this message in a reply
« Next Oldest | Next Newest »
Post Reply  Post Thread 

View printable version View a Printable Version
Send this thread to a friend Send this Thread to a Friend
Subscribe to thread Subscribe to this thread | Add to favourites
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Forum Jump: