Search
Username:
Password:

Register or Reset Password

SUBMISSIONS
GENERATORS
INSTALL HELP
DEVHUB
FOR MAPPERS
HELP WANTED
CLANS
SPONSORS
Countdown to date
Difficulty: Intermediate
Category: FPSB > ScriptingAuthor(s): kyle

Countdown to date
Countdown to a certain date in PHP.

Do you run a site and would like to let users know how many days are left until the next update of something comes out? or just want to countdown to a date? well, I'll be showing you how to accomplish this today.

Open your favorite text editor. For this tutorial, I'll be using Adobe Dreamweaver CS3. I recommend this program because not only is it amazing, it will also make your code syntax how it should be.

Start by declaring what kind of script it will be. You do this by adding <?php to the top of the script.

Next line down, create the function. This is done by typing out the following:

function countdown($event,$month,$day,$year) {

countdown: Is what the name of the function is called. You could name it whatever you'd like as long as you change it in the rest of the code to match what you named it. Otherwise, it wont work.
$event: Is what you're counting down to. E.g.: My Birthday, graduation, etc.
$month, $day, $year: These are pretty self explanatory.

Now that you've created the function, let's make it work.

$remain = ceil((mktime(0,0,0,$month,$day,$year) - time()) / 86400);


0,0,0,$month,$day,$year: Basically, what this is, is what it's counting down to. It goes in 24-hour format. The 0,0,0 would then be seconds, minutes, hour, etc. So by setting them to 0's, it will be an exact time. E.g.: 3:00 PM.
time()) / 86400: 86400 seconds are in 1 day.

Now we're going to have the script figure out if the even has arrived or not by doing the following:

if ($remain > 0) {
print "$remain more day(s) until $event";
}

else {
print
"$event has arrived!";
}
}


In a nutshell: The if/else statement you just created will have the script check if the even has arrived or not. If it has, it will display that it has. If it didn't, it will tell you how many month(s)/day(s)/year(s) are left.

You could then go on to create the rest of your website how you'd like. When you want to call the script, you could use;
<?php countdown("Christmas", 12, 25, 2008); ?>

Replacing countdown with whatever you decided to name the function.

Note: This is a very simple script. If you really wanted, you could go more into depth and create a dynamic script that will display the exact time left.
Added: 2 months agoTags: php, count, down, date
Feedback (5)
.
ˇViva la revolución!Y2M
Posted 1 month ago
Nice job! I tend to use echo instead of print >.<
s500i93 .
not Banned yet =DY2M
Posted 2 months ago
There is nothing to complain about, tested this code Rapid CSS 2007 and it worked perfect there
Bov .
NoobY2M
Posted 2 months ago
Some very nice stuff.
.
A witty saying proves nothing.Y2M
Posted 2 months ago
This tutorial is very well written, It even has syntax colouring!

10/10
El Tigre .
alex says: oooo! thats so RAD!Y2M
Posted 2 months ago
Pros: Good And readeble
Cons: none
Improvements: none
Notes: Really nice made
Add Feedback
Provide constructive feedback. What is "constructive"?
You cannot post yet!
Get involved on FPSB by posting your thoughts. Give authors your feedback to help them develop.

To post in this submission register or login.
Assessment
The community's verdict.
User Rating [+]
10/10
bScore
10.0/10
You cannot assess yet!
Moderating FPSB is in your hands - when you assess a submission your vote determines its success or failure.

To vote on this submission register or login.
Stamps
This Tutorial in a nutshell...
No stamps recorded
You cannot stamp yet!
Stamps let you apply characteristics to submissions to help others understand them.

To stamp this submission register or login.

Brought to you by...
Krazy Productions
Making games Krazy!
Membership: On Request
Primary Skills: 3D Animation, 3D Art & Rendering, Coding/Scripting, Compiling, Concept Art, Graphic Design, Mapping, Modelling, Texturing, Web Development
Inception: 6 months ago
Vote for Studio
Like this tut? Vote Krazy Productions for one or more monthly awards.
You must login or register to access this!
Related Tutorials