Version: 0.1-beta

Type: Class

Category: Calendars/Dates

License: GNU General Public License

Description: This first version of the Date class is for evaluation. It performs date arithmetic such as difference between two dates, adding days/hours/week/minutes/seconds, etc... This class needs enhancements for it cannot compute before 1970.



************CUT AND PASTE CODE AFTER THIS LINE TO dateclass_sample.php*********
<?php //---dateclass_sample.php

    //---Description:   Sample call to Date class
    //---Author:        Edwin C. Villapando  (jgreco@surfshop.net.ph)
    //---Version:       0.1-beta
    //---Date:          2001-12-29 22:30
    //---File:          dateclass_sample.php

    include ("Date.class");

    //---Instances of the Date class

    $dateSTR = new Date("2001-12-01");  //---passed with String date
    $date = new Date($dateSTR);         //---passed with Date object
    $dateINT = new Date(time());        //---passed with timestamp/integer
    $dateNULL = new Date();             //---no parameter, default is current time

    //---variants
    $dateTIMEONLY = new Date("21:45");              //---passed with time only
    $dateDATETIME = new Date("2001-12-29 22:30");   //---passed with date and time


    $leapyear = new Date("2000-01-01");
    $lastday = $date->lastdayofmonth();
    $firstday = $date->firstdayofmonth();


    echo "$date->CDATE is leapyear? = " . ($date->isleapyear() ? "Yes" : "No") . "\n";
    echo "$leapyear->CDATE is leapyear? = " . ($leapyear->isleapyear() ? "Yes" : "No") . "\n";

    $nextday = $date->dateadd(2);
    echo "2 day after $date->CDATE is $nextday->CDATE\n";

    echo "Days difference on lastday of $date->CDATE is " . $date->datediff($date->lastdayofmonth()) . " day(s)\n";

    echo "First day of $date->CDATE is $firstday->CDATE\n";
    echo "Last day of $date->CDATE is $lastday->CDATE\n";


    echo "Days difference between $date->CDATE and " .
         "$lastday->CDATE is " . $date->datediff($lastday) . " day(s) \n";

    $date->setdate($lastday);
    echo "\nSet date to new value = $date->CDATE\n";


    $firstday->setdate($firstday->firstdayofyear());
    $lastday->setdate($lastday->lastdayofyear());

    echo "First date is $firstday->CDATE\n";
    echo "Last date is $lastday->CDATE\n";

    echo "\nSample Time:\n\t\$dateTIMEONLY = new Date(\"21:45\");\t$dateTIMEONLY->CTIME\n";
    echo "\t\$dateDATETIME = new Date(\"2001-12-29 22:30\");\t$dateDATETIME->CTIME\n";

    echo "\nSome formatting:\n";
    echo "\t\$dateTIMEONLY = new Date(\"21:45\");\t" . $dateTIMEONLY->format("%I:%M:%S %p") . "\n";
    echo "\t\$dateDATETIME = new Date(\"2001-12-29 22:30\");\t" . $dateDATETIME->format("%I:%M:%S %p") . "\n";

?>
************END OF dateclass_sample.php DO NOT INCLUDE THIS LINE*********


************CUT AND PASTE CODE AFTER THIS LINE TO Date.class*********
<?php //---Date.class

    //---Description:   Date Class
    //---Author:        Edwin C. Villapando  (jgreco@surfshop.net.ph)
    //---Version:       0.1-beta
    //---Date:          2001-12-29 22:30
    //---File:          Date.class


    class Date {

        //---
        //---Date class variables
        //---

        var $date;          //---holds the actual date/time stamp
        var $SECONDS;       //---"seconds"
        var $MINUTES;       //---"minutes"
        var $HOURS;         //---"hours"
        var $DAY;           //---"mday"
        var $WEEKDAY;       //---"wday"
        var $CWEEKDAY;      //---"weekday"
        var $MONTH;         //---"mon"
        var $YEAR;          //---"year"
        var $DAYOFYEAR;     //---"yday"
        var $CMONTH;        //---"month"

        var $CDATE;         //---date rep. of $date "1970-01-01"
        var $CTIME;         //---time rep. of $date "11:30:45"

        //---end of Date class variables

        //---
        //---Date class methods
        //---
        
        function Date($date = NULL) {
        //--- Date class constructor

            //---populate the properties with values
            $this->init_date(getdate($this->eval_dparm($date)));
            
        } //---end of Date class constructor


        function eval_dparm($date) {
        //---Evaluate a date parameter and return a date/time stamp

            if (is_string($date)) {
                if (($date == "") || strtotime($date) == -1) {
                    //---String Date is passed but evaluates to non-date/time.
                    $tmpdate = time();
                } else {
                    //---Convert string date to date/time stamp
                    $tmpdate = strtotime($date);
                }

            } elseif (is_null($date))  {
                //---default to current date
                $tmpdate = time();

            } elseif (is_numeric($date)) {
                //---numeric date is passed
                $tmpdate = $date;

            } else {
                if (get_class($date) == "date") {
                    //---The passed parameter is a Date class
                    //---so, get the $obj->date property
                    $tmpdate = $date->date;
                } else {
                    //---Catch anything else, return current date
                    $tmpdate = time();
                }
            }

            return $tmpdate;

        } //---end of eval_dparm

        
        function init_date($date_arr) {
        //---Initialize the properties

            $this->date         = $date_arr[0];
            $this->SECONDS      = $date_arr["seconds"];
            $this->MINUTES      = $date_arr["minutes"];
            $this->HOURS        = $date_arr["hours"];
            $this->DAY          = $date_arr["mday"];
            $this->WEEKDAY      = $date_arr["wday"];
            $this->CWEEKDAY     = $date_arr["weekday"];
            $this->MONTH        = $date_arr["mon"];
            $this->YEAR         = $date_arr["year"];
            $this->DAYOFYEAR    = $date_arr["yday"];
            $this->CMONTH       = $date_arr["month"];

            $this->CDATE        = $this->format("%Y-%m-%d");
            $this->CTIME        = $this->format("%H:%M:%S");

        } //---end of init_date()


        function isleapyear() {
        //----Determine if the current Date object is a leap year

            return (($this->YEAR % 4) == 0);

        } //---end of isleapyear()


        function setdate($date = NULL) {
        //---Change the current date value of the Date object with new one
        //---without destroying the current Date object. Useful on loops

            $this->init_date(getdate($this->eval_dparm($date)));

        } //---end of setdate()


        function format($format = "%Y-%m-%d %H:%M:%S") {
        //---String format the Date object
        //---Use strftime() format mask

            //---default return value format "1970-01-01 11:30:45"
            return strftime($format, $this->date);

        } //---end of format()


        
        function datediff($date, $elaps = "d") {
        //---
        //---Returns number of weeks/days/hours/minutes/seconds
        //---between two dates/time. Default is "d" days to return
        //---
        //---Legend ($elaps):
        //---
        //---   w - weeks
        //---   d - days
        //---   h - hours
        //---   m - minutes
        //---   s - seconds
        //---

            $__DAYS_PER_WEEK__       = (7);
            $__HOURS_IN_A_DAY__      = (24);
            $__MINUTES_IN_A_DAY__    = (1440);
            $__SECONDS_IN_A_DAY__    = (86400);

            
            //---Compute days elaps. This is the default return value
            $__DAYSELAPS = ($this->eval_dparm($date) - $this->date) / $__SECONDS_IN_A_DAY__ ;

            switch ($elaps) {
                case "w":
                    //---Convert it to weeks
                    $__DAYSELAPS =  $__DAYSELAPS / $__DAYS_PER_WEEK__;
                    break;

                case "h":
                    //---Convert it to hours
                    $__DAYSELAPS =  $__DAYSELAPS * $__HOURS_IN_A_DAY__;
                    break;        

                case "m":
                    //---Convert it to minutes
                    $__DAYSELAPS =  $__DAYSELAPS * $__MINUTES_IN_A_DAY__;  
                    break;

                case "s":
                    //---Convert it to seconds
                    $__DAYSELAPS =  $__DAYSELAPS * $__SECONDS_IN_A_DAY__;
                    break;        
            } 

            return $__DAYSELAPS;

        } //---end of datediff()


        function firstdayofmonth() {
        //---Returns a Date object representating the first day of the
        //---month of the current Date object
        //---

            return (new Date(strftime("%Y-%m-%d", mktime(0, 0, 0,
                                   $this->MONTH,           
                                   1,              //---1 always the 1st day 
                                   $this->YEAR))));           
                                                   
        } //---end of firstdayofmonth()

        function firstdayofyear() {
        //---Returns a Date object representating the first day of the
        //---year of the current Date object
        //---

            return (new Date(strftime("%Y-%m-%d", mktime(0, 0, 0,
                                   1,              //---1 always the 1st month
                                   1,              //---1 always the 1st day 
                                   $this->YEAR))));           
                                                   
        } //---end of firstdayofyear()


        function lastdayofmonth() {
        //---Returns a Date object representating the last day of the
        //---month of the current Date object
        //---
    
            return (new Date(strftime("%Y-%m-%d", mktime(0, 0, 0,
                                   $this->MONTH + 1,    //---next month
                                   0,                   //---1-1=0 is the last day of the
                                   $this->YEAR))));     //---current month

        }  //---end of lastdayofmonth()


        function lastdayofyear() {
        //---Returns a Date object representating the last day of the
        //---year of the current Date object
        //---
    
            return (new Date(strftime("%Y-%m-%d", mktime(0, 0, 0,
                                   1,                   //---January
                                   0,                   //---1-1=0 is the last day 
                                   $this->YEAR + 1)))); //---next year

        }  //---end of lastdayofyear()



        function dateadd($number = 0, $interval = "d") {
        //---Returns Date object upon adding $interval/$number to
        //---current Date object. $number is the number to add
        //---
        //---Legend ($interval):
        //---
        //---   yyyy - year
        //---   q    - quarter
        //---   m    - month
        //---   y    - day of year
        //---   d    - day (default with $number = 0)
        //---   w    - weekday
        //---   ww   - week of year
        //---   h    - hour
        //---   n    - minute
        //---   s    - second
        //---    

    
            $hours =  $this->HOURS;
            $minutes =  $this->MINUTES;
            $seconds =  $this->SECONDS;
            $month =  $this->MONTH;
            $day =  $this->DAY;
            $year =  $this->YEAR;

            switch ($interval)
            {
                case "yyyy":
                    //---Add $number to year
                    $year += $number;
                    break;

                case "q":
                    //---Add $number to quarter
                    $year += ($number*3);
                    break;

                case "m":
                    //---Add $number to month
                    $month += $number;
                    break;

                case "y":
                case "d":
                case "w":
                    //---Add $number to day of year, day, day of week
                    $day += $number;
                    break;

                case "ww":
                    //---Add $number to week
                    $day += ($number*7);
                    break;

                case "h":
                    //---Add $number to hours
                    $hours += $number;
                    break;

                case "n":
                    //---Add $number to minutes
                    $minutes += $number;
                    break;

                case "s":
                    //---Add $number to seconds
                    $seconds += $number;
                    break;        
            } 
   
            return (new Date(mktime($hours,
                                    $minutes,
                                    $seconds,
                                    $month,
                                    $day,
                                    $year)));

        } //---end of dateadd()

        //---end of Date class methods

    } //---end of Date class

?> 
************END OF Date.class DO NOT INCLUDE THIS LINE*********