Version: 0.0.1
Type: Class
Category: Calendars/Dates
License: GNU General Public License
Description: Originally ctreated by sid trivedi on php manual.
<?php /************************************************************** # TITLE : Object Oriented Approach to countdown to any particular date or event * # Same procedural snipped is posted in Help Manual ---> date() # http://www.php.net/manual/en/function.date.php # If you compare both snippets, It would be easy to understand PHP-OOPs! ***************************************************************/ //Any of following input can be retrieved by GET OR POST. //Input is set for New Year event. $year = '2009'; $month= '01'; $day = '01'; $hour = '00'; $minute = '00'; $second = '00'; $date = array($hour,$minute,$second,$month,$day,$year); //Defining class bt keyword class class DateCountDown { //Class member definition - Easy to remember class members var $year = ""; var $month= ""; var $day = ""; var $hour = ""; var $minute = ""; var $second = ""; var $date = ""; var $dl = ""; var $hl = ""; var $ml = ""; var $sl = ""; var $return = ""; //Countdown to particular Function function countdown(){ global $return; global $countdown_date; global $date; list ($hour, $minute, $second, $month, $day, $year)= $date; $countdown_date = mktime($hour, $minute, $second, $month, $day, $year); $today = time(); $diff = $countdown_date - $today; if ($diff < 0)$diff = 0; $dl = floor($diff/60/60/24); $hl = floor(($diff - $dl*60*60*24)/60/60); $ml = floor(($diff - $dl*60*60*24 - $hl*60*60)/60); $sl = floor(($diff - $dl*60*60*24 - $hl*60*60 - $ml*60)); // OUTPUT $return = array($dl, $hl, $ml, $sl); return ($return); } } //Making New Object for the class CountDownDate $date_obj = new DateCountDown($date); //Function (Method) call by an object to function countdown $date_obj -> countdown(); //Making varialbes $dl => Days Left, $hl => Hours Left and so on from $return array list($dl,$hl,$ml,$sl) = $return; //Browser Output echo "Today's date ".date("F j, Y, g:i:s A")."<br/>"; echo "Countdown date ".date("F j, Y, g:i:s A",$countdown_date)."<br/>"; echo "n<br>"; echo "Countdown ".$dl." days ".$hl." hours ".$ml." minutes ".$sl." seconds left"."n<br>"; /* Above snippet produces following output: Today's date February 11, 2008, XX:XX:XX PM Countdown date January 1, 2009, 12:00:00 AM Countdown 324 days XX hours XX minutes XX seconds left */ ?>