# Creating a count-down system

Creating a count down system

In this tutorial I will show you how to create a simple count down system.

Step 1.
First of all let's collect what we need to make our idea to a working code. We want to calculate how many day, hours, minutes and seconds are until a given date is coming. To do this we need the date and time in the future (let's call it to target date) and the actual date and time informations. In this tutorial we will use a hard coded target date. To make our calculation more easy and to avoid any date/time string format problem just define each date component separate like this:

<?php
//Â DefineÂ yourÂ targetÂ dateÂ here
Â Â Â Â
\$targetYearÂ Â Â =Â 2007;
Â Â Â Â
\$targetMonthÂ Â =Â 9;
Â Â Â Â
\$targetDayÂ Â Â Â =Â 10;
Â Â Â Â
\$targetHourÂ Â Â =Â 12;
Â Â Â Â
\$targetMinuteÂ =Â 00;
Â Â Â Â
\$targetSecondÂ =Â 00;

?>

Step 2.
As next step we have to calculate the difference between the actual date and the target date. To do this we will convert/get both date values to a UNIX timestamp in a long integer format which containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified. To convert the target date use the built in PHP function mktime()

Synatx:
int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

As you can see we can use here the date components defined in Step 1.

<?php
\$targetDateÂ
=Â mktime(\$targetHour,\$targetMinute,\$targetSecond,\$targetMonth,\$targetDay,\$targetYear);
?>

To get the actual date we can simply use an oyher built in PHP function time() which function returns the actual date in UNIXtimestamp format as the mktime function.

Syntax:
int time ( void )

<?php
\$actualDateÂ
=Â time();
?>

Step 3.
Now we have both the dates so we can calculate the difference quite easy: target date - actual date. Let's make some calculation how many full days, hours, minutes is it. As the difference is in seconds we will use the following approach. A day is 24 hours, each hours has 60 minutes and each minute has 60 seconds so at the end a full day is 60*60*24 seconds. We want to get only the full days so during our calculation we will use the floor() function which returns the next lowest integer value by rounding down value if necessary. So the complete calculation process looks like this:

<?php

\$secondsDiffÂ
=Â \$targetDateÂ -Â \$actualDate;

\$remainingDayÂ Â Â Â Â =Â floor(\$secondsDiff/60/60/24);
\$remainingHourÂ Â Â Â =Â floor((\$secondsDiff-(\$remainingDay*60*60*24))/60/60);
\$remainingMinutesÂ =Â floor((\$secondsDiff-(\$remainingDay*60*60*24)-(\$remainingHour*60*60))/60);
\$remainingSecondsÂ =Â floor((\$secondsDiff-(\$remainingDay*60*60*24)-(\$remainingHour*60*60))-(\$remainingMinutes*60));

?>

Step 4.
As last step we makes some formatting. In this case I will display the target date, the actual date and the remaining time. To make a nice formatting let's define a fomatting string for the date() function.

Synatx:
string date ( string format [, int timestamp] )

So the code which formats the output is the following:

<?php
//Â DefineÂ dateÂ format
\$dateFormatÂ =Â "Y-m-dÂ H:i:s";

\$targetDateDisplayÂ =Â date(\$dateFormat,\$targetDate);
\$actualDateDisplayÂ =Â date(\$dateFormat,\$actualDate);
?>

And as result here is the complete PHP code:

<?php
//Â DefineÂ yourÂ targetÂ dateÂ here
Â Â Â Â
\$targetYearÂ Â Â =Â 2007;
Â Â Â Â
\$targetMonthÂ Â =Â 9;
Â Â Â Â
\$targetDayÂ Â Â Â =Â 10;
Â Â Â Â
\$targetHourÂ Â Â =Â 12;
Â Â Â Â
\$targetMinuteÂ =Â 00;
Â Â Â Â
\$targetSecondÂ =Â 00;
//Â EndÂ targetÂ dateÂ definition

\$targetDateÂ =Â mktime(\$targetHour,\$targetMinute,\$targetSecond,\$targetMonth,\$targetDay,\$targetYear);
\$actualDateÂ =Â time();

\$secondsDiffÂ =Â \$targetDateÂ -Â \$actualDate;

\$remainingDayÂ Â Â Â Â =Â floor(\$secondsDiff/60/60/24);
\$remainingHourÂ Â Â Â =Â floor((\$secondsDiff-(\$remainingDay*60*60*24))/60/60);
\$remainingMinutesÂ =Â floor((\$secondsDiff-(\$remainingDay*60*60*24)-(\$remainingHour*60*60))/60);
\$remainingSecondsÂ =Â floor((\$secondsDiff-(\$remainingDay*60*60*24)-(\$remainingHour*60*60))-(\$remainingMinutes*60));

//Â DefineÂ dateÂ format
\$dateFormatÂ =Â "Y-m-dÂ H:i:s";

\$targetDateDisplayÂ =Â date(\$dateFormat,\$targetDate);
\$actualDateDisplayÂ =Â date(\$dateFormat,\$actualDate);

?>

<!DOCTYPEÂ htmlÂ PUBLICÂ "-//W3C//DTDÂ XHTMLÂ 1.0Â Transitional//EN"Â "DTD/xhtml1-transitional.dtd">
<html>
<body>
Â TARGETÂ DATEÂ :Â <?phpÂ echoÂ \$targetDateDisplay;Â ?><br/><br/>
Â ACTUALÂ DATEÂ :Â <?phpÂ echoÂ \$actualDateDisplay;Â ?><br/><br/>
Â REMAININGÂ Â Â :Â <?phpÂ echoÂ "\$remainingDayÂ days,Â \$remainingHourÂ hours,Â \$remainingMinutesÂ minutes,Â \$remainingSecondsÂ seconds";?>
</body>Â Â Â