Countdown to date Difficulty: Intermediate |
| Category: FPSB > Scripting | Author(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 ago | Tags: php, count, down, date |
 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. |
 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. This Tutorial in a nutshell... No stamps recordedYou cannot stamp yet!Stamps let you apply characteristics to submissions to help others understand them. To stamp this submission register or login.
 |
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 |
Want more? Find related tuts. |