Re: Date-class

From: Date: Wed, 18 Jul 2001 06:24:35 +0000
Subject: Re: Date-class
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-857@lists.php.net to get a copy of this message
Hello. As for naming the variables do not use plural.
* $date->year() * $date->month() * $date->day() * $date->hours() * $date->minutes() * $date->seconds()
You dont have variable "moths" you have "month", so use hour instead on hours and minute and second instead of minutes & seconds. And as for OO try to write methods for get&set the variables instead of accessing them directly. Erik Hjortsberg wrote:
Ok, I've written a date class for making atomic date instances. The current Date classes do only provide static methods. There is however a need for a common format for handling dates in an application, especially in a multi-developer situation (such as PEAR). Anyhow, a short introduction * Instances are created by using the static Date::From* functions: * * $date = & Date::FromISO('2001-01-20 10:01:02'); * $date = & Date::FromISO('2001-01-20'); * $date = & Date::FromDatetime('20010120100102'); * $date = & Date::FromTimestamp(979981262); * * To get output from the object use the as*() functions * * $date->asISO() => '2001-01-20 10:01:02' * $date->asDate() => '2001-01-20' * $date->asTime() => '10:01:02' * $date->asDatetime() => 20010120100102 * $date->asShortDate() => '20/01 * $date->asDayInWeek() => 6 * $date->asWeekNumber() => 03 * $date->asTextualDay() => saturday (depending on your locale setting) * * You can also get and set more specific values * * $date->year() * $date->month() * $date->day() * $date->hours() * $date->minutes() * $date->seconds() * * Supply a parameter to set the value * * $date->year(2002) * $date->month(11) * * To compare two dates use *
*    $res = $date1->compareTo($date2);
I've chosen to use a timestamp as internal representation for now. That will probably change. I've also tried to keep the extra methods to a minium to keep it simple. If you want a special formatting you can do it with date('fsdafd', $date->timestamp()). Plus, the methods provided by Date_Date are also helpful. /erik hjortsberg ------------------------------------------------------------------------ <? /** * Date class * @version 0.9 * @author Erik Hjortsberg * Last modified 2001-07-18 * * $Id: Date.class.php,v. 1.0 * * Description: * * A class for handling dates a atomic objects. * Instances are created by using the static Date::From* functions: * * $date = & Date::FromISO('2001-01-20 10:01:02'); * $date = & Date::FromISO('2001-01-20'); * $date = & Date::FromDatetime('20010120100102'); * $date = & Date::FromTimestamp(979981262); * * To get output from the object use the as*() functions * * $date->asISO() => '2001-01-20 10:01:02' * $date->asDate() => '2001-01-20' * $date->asTime() => '10:01:02' * $date->asDatetime() => 20010120100102 * $date->asShortDate() => '20/01 * $date->asDayInWeek() => 6 * $date->asWeekNumber() => 03 * $date->asTextualDay() => saturday (depending on your locale setting) * * You can also get and set more specific values * * $date->year() * $date->month() * $date->day() * $date->hours() * $date->minutes() * $date->seconds() * * Supply a parameter to set the value * * $date->year(2002) * $date->month(11) * * To compare two dates use *
*    $res = $date1->compareTo($date2);
* */ define('DATE_LESS', -1); define('DATE_EQUAL', 0); define('DATE_LARGER', 1); class Date extends PEAR{
    //all variables are declared here
    //
    //public:
    //
    var $className = 'Date';
        //
    //private:
    //
    var $_timestamp; //(int) internal representation of the date,         //should perhaps be something that will stand the test of time better
        //the usefulness of timestamp is however that we can use mktime()
        //wich has excellent routines for adding and subtracting values out-of-range
        //There is however the problem with OSs only supporting 32-bit integers...
    
    /**     * Constructor     * In most cases you should use the Date::From*() methods instead
    * @param string (timestamp)
    *     */     function Date($timestamp = null)     {
        $this->PEAR();
        //if no timestamp we choose the date of today
        if ($timestamp === null) {
            $this->_timestamp = mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y'));
        } else {
            $this->_timestamp = $timestamp;
        }
    }
        // //---------public entity functions begins here // 
            /**     * Returns the time portion ("10:11:12" or "10:11".
    * @param boolean
    *                   if false, then only the hour and minute will be returned
    * @return string
    * @access public     *
    */     function asTime($withSeconds = true)     {
        if ($withSeconds) {
            return date('H:i:s', $this->_timestamp);
        } else {
            return date('H:i', $this->_timestamp);
        }
    }
    /**     * Returns the date portion. ("2001-01-02")
    * @return string
    * @access public     *     */     function asDate()     {
        return date('Y-m-d', $this->_timestamp);
    }
    /**     * Returns the date portion. ("2001-01-02")
    * @return string
    * @access public     *     */     function asShortDate()     {
        return date('d/m', $this->_timestamp);
    }
        /**     * Returns an ISO-8601 date. ("2001-01-02 10:11:12")
    * @return string
    * @access public     *     */     function asISO()     {
        return $this->asDate() . ' ' . $this->asTime();
    }
    /**     * Returns a datetime. ("20010102101112");
    * @return string
    * @access public     *     */     function asDatetime()     {
        return date("YmdHis", $this->_timestamp);
    }
    /**     * Returns the day of the week in numeric form;
    * @return int
    * @access public     *     */     function asDayInWeek()     {
        return (int) date('w', $this->_timestamp);
    }
            /**     * Returns the week number;
    * @return string
    * @access public     *     */     function asWeekNumber()     {
        /*
        $week_year = $this->year() - 1501;
        $week_day = $week_year * 365 + floor($week_year / 4) - 29872 + 1
            - floor($week_year / 100) + floor(($week_year - 300) / 400);
                 $week_number = floor((date('z', $this->_timestamp) + floor(($week_day + 4) % 7)) / 7);
        */
        /*
        Should be:
        $week_number = strftime('%V', $this->timestamp());
        as it's ISO 8601:1988, but it doesn't seem to work...
        */
        $week_number = strftime('%W', $this->timestamp());
        return $week_number;
    }         /**     * Returns the day of the week in textual form and according to the current locale.
    * @return int
    * @access public     *     */     function asTextualDay()     {
        return strftime('%A', $this->timestamp());
    }
    /**     * Compares two dates.
    *   If the local date is lesser it will return -1
    *   If the local date is larger it will return 1
    *   If the dates are equal it will return 0
    *   If $onlyDate is set it will only compare the date portion, else it will return both the date and time portions
    *
    * @param object (Date)
    * @param boolean
    * @return int
    * @access public     *     */     function compareTo(&$objDate, $onlyDate = false)     {
        if ($onlyDate) {
            $res = strcmp($this->asDate(), $objDate->asDate());
        } else {
            $res = strcmp($this->timestamp(), $objDate->timestamp());
        }
        return $res;
    }
        /**     * Creates and returns a copy of the current object
    *
    * @return object (Date)
    * @access public     *     */     function &clone()     {
        return new $this->className ($this->timestamp());
    }
        /**     * Sets the object to the date of the first day of it's week
    * @param int
    * @access public     *     */     function toBeginningOfWeek($weekStart =  1)     {
        $dayOfWeek = date('w', $this->_timestamp) - $weekStart;
        if ($dayOfWeek) {
            $this->day($this->day() - $dayOfWeek);
        }
    }           
    /**     * Returns or sets the timestamp.
    *
    * @param string
    * @return string
    * @access public     *     */     function timestamp($value = null)     {
        if ($value !== null) {
            $this->_timestamp = $value;
        }
        return $this->_timestamp;
    }
    /**     * Returns or sets the year.
    *
    * @param int
    * @return int
    * @access public     *     */     function year($value = null)     {
        if ($value !== null) {
            $this->_timestamp = mktime($this->hours() ,$this->minutes(), $this->seconds(),$this->month() ,$this->day(), $value);
        }
        return date('Y', $this->_timestamp);
    }
        /**     * Returns or sets the month.
    *
    * @param int
    * @return int
    * @access public     *     */     function month($value = null)     {
        if ($value !== null) {
            $this->_timestamp = mktime($this->hours() ,$this->minutes(), $this->seconds(), $value, $this->day(), $this->year());
        }
        return date('n', $this->_timestamp);
    }
        /**     * Returns or sets the day.
    *
    * @param int
    * @return int
    * @access public     *     */     function day($value = null)     {
        if ($value !== null) {
            $this->_timestamp = mktime($this->hours(), $this->minutes(), $this->seconds(), $this->month(), $value, $this->year());
        }
        return date('j', $this->_timestamp);
    }
        /**     * Returns or sets the hour.
    *
    * @param int
    * @return int
    * @access public     *     */     function hours($value = null)     {
        if ($value !== null) {
            $this->_timestamp = mktime($value ,$this->minutes(), $this->seconds(), $this->month(), $this->day(), $this->year());
        }
        return date('H', $this->_timestamp);
    }
        /**     * Returns or sets the minute.
    *
    * @param int
    * @return int
    * @access public     *     */     function minutes($value = null)     {
        if ($value !== null) {
            $this->_timestamp = mktime($this->hours(), $value, $this->seconds(), $this->month(), $this->day(), $this->year());
        }
        return date('i', $this->_timestamp);
    }
        /**     * Returns or sets the second.
    *
    * @param int
    * @return int
    * @access public     *     */     function seconds($value = null)     {
        if ($value !== null) {
            $this->_timestamp = mktime($this->hours() ,$this->minutes(), $value, $this->month(), $this->day(), $this->year());
        }
        return date('s', $this->_timestamp);
    }
    
// //---------private entity functions begins here // 
    // //---------public static functions begins here //         /**     * Creates a new Date-object from an ISO-8601 formatted date, YYYY-MM-DD HH:MM:SS
    * It will accept only the date portion too.
    *
    * @param string
    * @return object (Date)
    * @access public     * @static
    *
    */     function &FromISO($inputDate)     {
        $parts = explode(' ', $inputDate);
        if (count($parts) == 1) {
            $dateString = $parts[0] . ' ' . '01:01:01';
        } else {
            $dateString = $inputDate;
        }
        $timestamp = strtotime($dateString);
        if ($timestamp == -1) {
            //Date::RaiseError();
            //or
            return false;
        }
        return new Date($timestamp);
    }
        /**     * Creates a new Date-object from an UNIX timestamp
    *
    * @param int
    * @return object (Date)
    * @access public     * @static
    *
    */     function &FromTimestamp($inputTimestamp)     {
        //should add some kind of error-checking
        return new Date($inputTimestamp);
    }
    /**     * Creates a new Date-object from an datetime, YYYYMMDDHHMMSS
    *
    * @param string
    * @return object (Date)
    * @access public     * @static
    *
    */     function &FromDatetime($inputDatetime)     {           if (14 != strlen($inputDatetime)) {
            //PEAR::RaiseError() ??
            //or
            return false;
        }
        $timestamp = mktime(substr($inputDatetime,8,2), substr($inputDatetime,10,2), substr($inputDatetime,12,2), substr($inputDatetime,4,2), substr($inputDatetime,6,2), substr($inputDatetime,0,4));
        return new Date($timestamp);
    }
    // //---------private static functions begins here // 
        }
    
?> ------------------------------------------------------------------------ Date.class.php Content-Type: text/plain Content-Encoding: 7BIT ------------------------------------------------------------------------ Part 1.3 Content-Type: text/plain Content-Encoding: 7BIT


« previous php.pear.dev (#857) next »