Note: This is for those who already know about either setting up PHP on their own computer or have a server to which they can upload the following code. Check out w3schools for more info on PHP. http://w3schools.com/php/
The basic concept is to find a way to subtract two dates giving you a 'countdown.' This can be done plenty of ways I'm sure, but the easiest for me was to turn both dates into what's called a Unix timestamp. A Unix timestamp is the number of seconds since January 1, 1970.
1. First lets turn our destination date (the date we want to countdown to) into a Unix timestamp by using a PHP function called strtotime().
1. First lets turn our destination date (the date we want to countdown to) into a Unix timestamp by using a PHP function called strtotime().
strtotime("6/5/2010")
This will turn June 5th, 2010 into this: 1275714000.
2. Get the current Unix timestamp for this exact moment in time. This is made easy through the use of the function time()
time()
This exactly as it is above will return the Unix timestamp for that precise moment.
3. Now we can subtract the two amounts of seconds to get the number of seconds between now and our destination date.
strtotime("6/5/2010") - time()
4. In order to get the number of days from that amount of seconds we simply convert those seconds to days by division as follows. Lets say our seconds returned from the above calculation equals 172800. Take that number and divide it by the number of seconds that are in a day, 86400.
I can never remember how many seconds are in a day (I said I was a nerd...but come on). So a simple solution is to divide the seconds that are returned by this: (60 x 60 x 24). 60 seconds multiplied by 60 equals the seconds that are in an hour. Times that by 24 and that gives you the seconds in a day. The two statements below work the same.
$days = (strtotime("6/5/2010") - time())/(60*60*24); $days = (strtotime("6/5/2010") - time())/86400;
5. Next will be to round this number because it will be returned as a decimal such as 1.78483. So we use the ceil() function to round up.
$days = ceil((strtotime("6/5/2010") - time())/(60*60*24));
6. Then simply display the variable we just filled.
echo $days;
All together now.
$days = ceil((strtotime("6/5/2010") - time())/(60*60*24)); $s=''; if ($days!=1) { $s='s'; } echo $days. " day$s till we get married!";
Note the $s variable is to determine if I should make the word 'day' plural or singular upon writing it to the page. It basically checks to see if the $days variable equals 1 or not. If it does the word 'day' will be written. If not, 'days' will be written. Pretty simple.
Well hope this helps you countdown the days or basically whatever you want to count, until the special date.
Thanks for the quick and simple solution!
ReplyDeleteGlad this was able to help you out. :)
DeleteNice work, thanks Will. Just used this for a countdown to christmas thing I'm doing on a site :)
ReplyDeleteThanks
ReplyDeleteThanks it was really helpful and you posted this in very detail.
ReplyDeleteNice work there buddy!
ReplyDeleteJust used this! Great tutorial for PHP and using a simple method.
ReplyDeleteThank you all for viewing this tutorial. I'm glad you were able to use it.
ReplyDeleteThank you very much for this helpful tutorial...
ReplyDeleteNice work, helpful , thank you very much.
ReplyDelete