Your Ad Here


Go Back   VIPrapid.com > WEBMASTERS ZONE > Scripts

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-04-2009, 06:41 PM
VIP
 

Join Date: Mar 2009
Location: Before Internet
Posts: 80
Arrow Creating a 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 Code:
<?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 Code:
<?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 Code:
<?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 Code:
 <?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 Code:
<?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 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>
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT. The time now is 04:20 PM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.0
Ad Management by RedTyger
eXTReMe Tracker