If your site focuses on a specific event such as Christmas or your wedding, you may want to have a count down to let users know how long it will be until the event occurs. We can do this using timestamps and the mktime function.
Let's get going
First we need to set our target date. For our example we will use February 10th, 2007. We would get that with this line:
$target = mktime(0, 0, 0, 2, 10, 2007);
Next we need to get the current date. We can do that with this line:
$today = time ();
We now have to find the difference between them. To do that we simply need to subtract:
$difference = ($target-$today);
Since the timestamp is measured in seconds, we convert this into whatever units we want. If we want hours we can divide by 3600, however in thisexample we will be using days so we need to divide by 86400 (the number of seconds in a day). Floor simply rounds down.
$days = floor($difference/86400);
And there we have it!
$target = mktime(0, 0, 0, 2, 10, 2007);
$today = time ();
$difference = ($target-$today);
$days = floor($difference/86400);
echo "The event will occur in $days days";