eZ Publish  [trunk]
ezdate.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZDate class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZDate ezdate.php
00013   \ingroup eZLocale
00014   \brief Locale aware date handler
00015 
00016   eZDate handles date values in months, days and years.
00017   The time stored as a timestamp with 0 hours, 0 minutes and 0 seconds.
00018 
00019   A new instance of eZDate will automaticly use the current locale and current date,
00020   if you however want a different locale use the setLocale() function. The current locale can be
00021   fetched with locale().
00022 
00023   Change the date directly with setYear(), setMonth(), setDay() and setMDY().
00024   You can also adjust the date relative to it's current value by using adjustDate().
00025   Use timeStamp() to get the current timestamp value or year(), month() and day()
00026   for the respective values.
00027 
00028   When creating new times you're advised to use the static create()
00029   function which returns a new eZDate object. You can also create a copy
00030   with the duplicate() function.
00031 
00032   Date checking is done with the isGreaterThan() and isEqualTo() functions.
00033 
00034   Text output is done with toString() which can return a long string (default) or
00035   short string representation according to the current locale.
00036 
00037 Example:
00038 \code
00039 
00040 $us_locale = eZLocale::instance( 'us' );
00041 
00042 $date1 = new eZDate();
00043 $date2 = eZDate::create();
00044 $date2->setLocale( $us_locale );
00045 $date2->adjustDate( 1, 2, 3 );
00046 $date3 = $date1->duplicate();
00047 
00048 print( $date1->toString() );
00049 print( $date2->toString( true ) );
00050 print( $date1->isEqualTo( $date3 ) ? 'true' : 'false' ); // Prints 'true'
00051 
00052 \endcode
00053 
00054   \sa eZTime, eZDateTime, eZLocale
00055 */
00056 
00057 class eZDate
00058 {
00059     /*!
00060      Creates a new date object with default locale, if $date is not supplied
00061      the current date is used.
00062     */
00063     function eZDate( $date = false )
00064     {
00065         if ( $date === false )
00066         {
00067             $date = mktime( 0, 0, 0 );
00068         }
00069         else
00070         {
00071             $arr = getdate( $date );
00072             $date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
00073         }
00074         $this->Date = $date;
00075         $this->Locale = eZLocale::instance();
00076         $this->IsValid = $date > 0;
00077     }
00078 
00079     function attributes()
00080     {
00081         return array( 'timestamp',
00082                       'is_valid',
00083                       'year',
00084                       'month',
00085                       'day' );
00086     }
00087 
00088     function hasAttribute( $name )
00089     {
00090         return in_array( $name, $this->attributes() );
00091     }
00092 
00093     function attribute( $name )
00094     {
00095         if ( $name == 'timestamp'  )
00096             return $this->timeStamp();
00097         else if ( $name == 'is_valid' )
00098             return $this->isValid();
00099         else if ( $name == 'day'  )
00100             return $this->day();
00101         else if ( $name == 'year'  )
00102             return $this->year();
00103         else if ( $name == 'month'  )
00104             return $this->month();
00105 
00106         eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
00107         return false;
00108     }
00109 
00110     /*!
00111      \return true if the date has valid data.
00112     */
00113     function isValid()
00114     {
00115         return $this->IsValid;
00116     }
00117 
00118     /*!
00119      Sets the locale to $locale which is used in text output.
00120     */
00121     function setLocale( $locale )
00122     {
00123         $this->Locale = $locale;
00124     }
00125 
00126     /*!
00127      Returns the current locale.
00128     */
00129     function locale()
00130     {
00131         return $this->Locale;
00132     }
00133 
00134     /*!
00135      Returns the timestamp value, this is the number of seconds since the epoch
00136      with hours, minutes and seconds set to 0.
00137      \note The value is returned as a reference and should not be modified.
00138     */
00139     function timeStamp()
00140     {
00141         return $this->Date;
00142     }
00143 
00144     function setTimeStamp( $stamp )
00145     {
00146         $this->Date = $stamp;
00147         $this->IsValid = $stamp > 0;
00148     }
00149 
00150     /*!
00151      Sets the year leaving the other elements untouched.
00152     */
00153     function setYear( $year )
00154     {
00155         $arr = getdate( $this->Date );
00156         $this->Date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $year );
00157     }
00158 
00159     /*!
00160      Sets the month leaving the other elements untouched.
00161     */
00162     function setMonth( $month )
00163     {
00164         $arr = getdate( $this->Date );
00165         $this->Date = mktime( 0, 0, 0, $month, $arr['mday'], $arr['year'] );
00166     }
00167 
00168     /*!
00169      Sets the day leaving the other elements untouched.
00170     */
00171     function setDay( $day )
00172     {
00173         $arr = getdate( $this->Date );
00174         $this->Date = mktime( 0, 0, 0, $arr['mon'], $day, $arr['year'] );
00175     }
00176 
00177     /*!
00178      Returns the year element.
00179     */
00180     function year()
00181     {
00182         return date( 'Y', $this->Date );
00183     }
00184 
00185     /*!
00186      Returns the month element.
00187     */
00188     function month()
00189     {
00190         return date( 'm', $this->Date );
00191     }
00192 
00193     /*!
00194      Returns the day element.
00195     */
00196     function day()
00197     {
00198         return date( 'd', $this->Date );
00199     }
00200 
00201     /*!
00202      Sets the year, month and day elements. If $day or $year is omitted or set 0
00203      they will get a value taken from the current time.
00204     */
00205     function setMDY( $month, $day = 0, $year = 0 )
00206     {
00207         if ( $year != 0 )
00208             $date = mktime( 0, 0, 0, $month, $day, $year );
00209         else if ( $day != 0 )
00210             $date = mktime( 0, 0, 0, $month, $day );
00211         else
00212             $date = mktime( 0, 0, 0, $month );
00213         $this->Date = $date;
00214     }
00215 
00216     /*!
00217      Adjusts the date relative to it's current value. This is useful for adding/subtracting
00218      years, months or days to an existing date.
00219     */
00220     function adjustDate( $month, $day = 0, $year = 0 )
00221     {
00222         $arr = getdate( $this->Date );
00223         $date = mktime( 0, 0, 0, $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] );
00224         $this->Date = $date;
00225     }
00226 
00227     /*!
00228      Returns true if this object has a date greater than $date. $date can be specified as
00229      a timestamp value or as an eZDate object. If $equal is true it returns true if
00230      they are equal as well.
00231     */
00232     function isGreaterThan( $date, $equal = false )
00233     {
00234         $d1 = $this->timeStamp();
00235         if ( $date instanceof eZDate )
00236         {
00237             $d2 = $date->timeStamp();
00238         }
00239         else
00240         {
00241             $arr = getdate( $date );
00242             $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
00243         }
00244         if ( $d1 > $d2 )
00245             return true;
00246         else if ( $equal and $d1 == $d2 )
00247             return true;
00248         else
00249             return false;
00250     }
00251     /*!
00252      Returns true if this object is equal to $date. $date can be specified as
00253      a timestamp value or as an eZDate object.
00254     */
00255     function isEqualTo( $date )
00256     {
00257         $d1 = $this->timeStamp();
00258         if ( $date instanceof eZDate )
00259         {
00260             $d2 = $date->timeStamp();
00261         }
00262         else
00263         {
00264             $arr = getdate( $date );
00265             $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
00266         }
00267         return $d1 == $d2;
00268     }
00269 
00270     /*!
00271      Creates a new eZDate object with the date values $month, $day and $year and returns a reference to it.
00272      Any value can be ommitted or set to 0 to use the current date value.
00273     */
00274     static function create( $month, $day = 0, $year = 0 )
00275     {
00276         if ( $year != 0 )
00277             $date = mktime( 0, 0, 0, $month, $day, $year );
00278         else if ( $day != 0 )
00279             $date = mktime( 0, 0, 0, $month, $day );
00280         else
00281             $date = mktime( 0, 0, 0, $month );
00282         $newDateObject = new eZDate( $date );
00283         return $newDateObject;
00284     }
00285 
00286     /*!
00287      \deprecated This function is deprecated in PHP5, use the PHP5 clone keyword instead
00288      Creates an exact copy of this object and returns it.
00289     */
00290     function duplicate()
00291     {
00292         $copy = clone $this;
00293         return $copy;
00294     }
00295 
00296     /*!
00297      Creates a string representation of the date using the current locale and returns it.
00298      If $short is true a short representation is used.
00299     */
00300     function toString( $short = false )
00301     {
00302         if ( $short )
00303             $str = $this->Locale->formatShortDate( $this->Date );
00304         else
00305             $str = $this->Locale->formatDate( $this->Date );
00306         return $str;
00307     }
00308 
00309 
00310     /// Locale object, is just a reference to minimize memory usage.
00311     public $Locale;
00312     /// The current date as a timestamp without hour, minute or second values
00313     public $Date;
00314     public $IsValid;
00315 }
00316 
00317 ?>