eZ Publish  [4.0]
ezdatetime.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZDateTime class
00004 //
00005 // Created on: <01-Mar-2002 13:48:12 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZDateTime ezdatetime.php
00033   \ingroup eZLocale
00034   \brief Locale aware date and time handler
00035 
00036   eZDateTime handles 24 hour time values in hours, minutes and seconds
00037   and date values.
00038   The datetime stored as a timestamp with the number of seconds since the epoch.
00039   See PHP function date() and time() for more information.
00040 
00041   A new instance of eZDateTime will automaticly use the current locale and current datetime,
00042   if you however want a different locale use the setLocale() function. The current locale can be
00043   fetched with locale().
00044 
00045   Change the time directly with setHour(), setMinute(), setSecond() and setHMS().
00046   Change the date directly with setYear(), setMonth(), setDay() and setMDY().
00047   You can also adjust the date time relative to it's current value by using
00048   adjustDateTime(). Use timeStamp() to get the current timestamp value or
00049   year(), month(), day(), hour(), minute() and second() for the respective
00050   values.
00051 
00052   When creating new datetimes you're advised to use the static create()
00053   function which returns a new eZDateTime object. You can also create a copy
00054   with the duplicate() function.
00055 
00056   Time checking is done with the isGreaterThan() and isEqualTo() functions.
00057 
00058   Text output is done with toString() which can return a long string (default) or
00059   short string representation according to the current locale.
00060 
00061 Example:
00062 \code
00063 //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00064 //include_once( 'lib/ezlocale/classes/ezdatetime.php' );
00065 
00066 $us_locale = eZLocale::instance( 'us' );
00067 
00068 $dt1 = new eZDateTime();
00069 $dt2 = eZDateTime::create();
00070 $dt2->setLocale( $us_locale );
00071 $dt2->adjustDateTime( -8, 0, 0, 1, 2, 3 );
00072 $dt3 = $dt1->duplicate();
00073 
00074 print( $dt1->toString() );
00075 print( $dt2->toString( true ) );
00076 print( $dt1->isEqualTo( $dt3 ) ? 'true' : 'false' ); // Prints 'true'
00077 
00078 \endcode
00079 
00080   \sa eZDate, eZTime, eZLocale
00081 */
00082 
00083 //include_once( 'lib/ezlocale/classes/ezlocale.php' );
00084 //include_once( 'lib/ezlocale/classes/ezdate.php' );
00085 //include_once( 'lib/ezlocale/classes/eztime.php' );
00086 
00087 class eZDateTime
00088 {
00089     /*!
00090      Creates a new datetime object with default locale, if $datetime is not supplied
00091      the current datetime is used.
00092     */
00093     function eZDateTime( $datetime = false )
00094     {
00095         if ( $datetime instanceof eZDate )
00096         {
00097             $arr = getdate( $datetime->timeStamp() );
00098             $arr2 = getdate( $this->DateTime );
00099             $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'],
00100                                 $arr['mon'], $arr['mday'], $arr['year'] );
00101         }
00102         else if ( $datetime instanceof eZTime )
00103         {
00104             $arr2 = getdate( $datetime->timeStamp() );
00105             $arr = getdate( $this->DateTime );
00106             $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'],
00107                                 $arr['mon'], $arr['mday'], $arr['year'] );
00108         }
00109         else if ( $datetime === false )
00110         {
00111             $datetime = time();
00112         }
00113         else
00114         {
00115             $arr = getdate( $datetime );
00116             $datetime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
00117                                 $arr['mon'], $arr['mday'], $arr['year'] );
00118         }
00119         $this->DateTime = $datetime;
00120         $this->Locale = eZLocale::instance();
00121         $this->IsValid = $datetime > 0;
00122     }
00123 
00124     function attributes()
00125     {
00126         return array( 'timestamp',
00127                       'hour',
00128                       'minute',
00129                       'year',
00130                       'month',
00131                       'day',
00132                       'is_valid' );
00133     }
00134 
00135     function hasAttribute( $name )
00136     {
00137         return in_array( $name, $this->attributes() );
00138     }
00139 
00140     function attribute( $name )
00141     {
00142         if ( $name == 'timestamp'  )
00143         {
00144             return $this->timeStamp();
00145         }
00146         else if ( $name == 'hour' )
00147         {
00148             return $this->hour();
00149         }
00150         else if ( $name == 'minute'  )
00151         {
00152             return $this->minute();
00153         }
00154         else if ( $name == 'day'  )
00155         {
00156             return $this->day();
00157         }
00158         else if ( $name == 'year'  )
00159         {
00160             return $this->year();
00161         }
00162         else if ( $name == 'month'  )
00163         {
00164             return $this->month();
00165         }
00166         else if ( $name == 'is_valid'  )
00167         {
00168             return $this->isValid();
00169         }
00170         else
00171         {
00172             eZDebug::writeError( "Attribute '$name' does not exist", 'eZDateTime::attribute' );
00173             return false;
00174         }
00175     }
00176 
00177     /*!
00178      \return true if the date has valid data.
00179     */
00180     function isValid()
00181     {
00182         return $this->IsValid;
00183     }
00184 
00185     /*!
00186      Sets the locale to $locale which is used in text output.
00187     */
00188     function setLocale( $locale )
00189     {
00190         $this->Locale = $locale;
00191     }
00192 
00193     /*!
00194      Returns the current locale.
00195     */
00196     function locale()
00197     {
00198         return $this->Locale;
00199     }
00200 
00201     /*!
00202      Returns the current time zone.
00203     */
00204     function timeZone()
00205     {
00206         return date( 'T', $this->DateTime );
00207     }
00208 
00209     /*!
00210      Returns the timestamp value, this is the number of seconds since the epoch.
00211      \note The value is returned as a reference and should not be modified.
00212     */
00213     function timeStamp( )
00214     {
00215         return $this->DateTime;
00216     }
00217 
00218     function setTimeStamp( $stamp )
00219     {
00220         $this->DateTime = $stamp;
00221         $this->IsValid = $stamp > 0;
00222     }
00223 
00224     /*!
00225      \static
00226      Returns the current date and time as a UNIX timestamp
00227     */
00228     static function currentTimeStamp()
00229     {
00230         return time();
00231     }
00232 
00233     /*!
00234      Creates an eZDate object of this datetime with the same date and locale.
00235      Returns a reference to the object.
00236     */
00237     function toDate()
00238     {
00239         $date = new eZDate( $this->DateTime );
00240         $date->setLocale( $this->Locale );
00241         return $date;
00242     }
00243 
00244     /*!
00245      Creates an eZTime object of this datetime with the same time and locale.
00246      Returns a reference to the object.
00247     */
00248     function toTime()
00249     {
00250         $time = new eZTime( $this->DateTime );
00251         $time->setLocale( $this->Locale );
00252         return $time;
00253     }
00254 
00255     /*!
00256      Returns the year element.
00257     */
00258     function year()
00259     {
00260         return date( 'Y', $this->DateTime );
00261     }
00262 
00263     /*!
00264      Returns the month element.
00265     */
00266     function month()
00267     {
00268         return date( 'm', $this->DateTime );
00269     }
00270 
00271     /*!
00272      Returns the day element.
00273     */
00274     function day()
00275     {
00276         return date( 'd', $this->DateTime );
00277     }
00278 
00279     /*!
00280      Returns the hour element.
00281     */
00282     function hour()
00283     {
00284         return date( 'G', $this->DateTime );
00285     }
00286 
00287     /*!
00288      Returns the minute element.
00289     */
00290     function minute()
00291     {
00292         return date( 'i', $this->DateTime );
00293     }
00294 
00295     /*!
00296      Returns the second element.
00297     */
00298     function second()
00299     {
00300         return date( 's', $this->DateTime );
00301     }
00302 
00303     /*!
00304      Sets the year leaving the other elements untouched.
00305     */
00306     function setYear( $year )
00307     {
00308         $arr = getdate( $this->DateTime );
00309         $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
00310                                   $arr['mon'], $arr['mday'], $year );
00311     }
00312 
00313     /*!
00314      Sets the month leaving the other elements untouched.
00315     */
00316     function setMonth( $month )
00317     {
00318         $arr = getdate( $this->DateTime );
00319         $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
00320                                   $month, $arr['mday'], $arr['year'] );
00321     }
00322 
00323     /*!
00324      Sets the day leaving the other elements untouched.
00325     */
00326     function setDay( $day )
00327     {
00328         $arr = getdate( $this->DateTime );
00329         $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
00330                                   $arr['mon'], $day, $arr['year'] );
00331     }
00332 
00333     /*!
00334      Sets the hour leaving the other elements untouched.
00335     */
00336     function setHour( $hour )
00337     {
00338         $arr = getdate( $this->DateTime );
00339         $this->DateTime = mktime( $hour, $arr['minutes'], $arr['seconds'],
00340                                   $arr['mon'], $arr['mday'], $arr['year'] );
00341     }
00342 
00343     /*!
00344      Sets the minute leaving the other elements untouched.
00345     */
00346     function setMinute( $min )
00347     {
00348         $arr = getdate( $this->DateTime );
00349         $this->DateTime = mktime( $arr['hours'], $min, $arr['seconds'],
00350                                   $arr['mon'], $arr['mday'], $arr['year'] );
00351     }
00352 
00353     /*!
00354      Sets the second leaving the other elements untouched.
00355     */
00356     function setSecond( $sec )
00357     {
00358         $arr = getdate( $this->DateTime );
00359         $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $sec,
00360                                   $arr['mon'], $arr['mday'], $arr['year'] );
00361     }
00362 
00363     /*!
00364      Sets all hour, minute and second elements leaving the other elements untouched.
00365     */
00366     function setHMS( $hour, $min = 0, $sec = 0 )
00367     {
00368         $arr = getdate( $this->DateTime );
00369         $this->DateTime = mktime( $hour, $min, $sec,
00370                                   $arr['mon'], $arr['mday'], $arr['year'] );
00371     }
00372 
00373     /*!
00374      Sets all hour, minute and second elements leaving the other elements untouched.
00375     */
00376     function setMDYHMS( $month, $day, $year, $hour, $min, $sec = 0 )
00377     {
00378         $this->DateTime = mktime( $hour, $min, $sec, $month, $day, $year );
00379     }
00380 
00381     /*!
00382      Sets the year, month and day elements. If $day or $year is omitted or set 0
00383      they will get a value taken from the current time.
00384     */
00385     function setMDY( $month, $day = 0, $year = 0 )
00386     {
00387         $arr = getdate( $this->DateTime );
00388         if ( $year != 0 )
00389             $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
00390                             $month, $day, $year );
00391         else if ( $day != 0 )
00392             $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
00393                             $month, $day );
00394         else
00395             $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
00396                             $month );
00397         $this->DateTime = $date;
00398     }
00399 
00400     /*!
00401      Adjusts the datetime relative to it's current value. This is useful for adding/subtracting
00402      hours, minutes, seconds, years, months or days to an existing datetime.
00403     */
00404     function adjustDateTime( $hour, $minute = 0, $second = 0, $month = 0, $day = 0, $year = 0 )
00405     {
00406         $arr = getdate( $this->DateTime );
00407         $date = mktime( $hour + $arr['hours'], $minute + $arr['minutes'], $second + $arr['seconds'],
00408                         $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] );
00409         $this->DateTime = $date;
00410     }
00411 
00412     /*!
00413      Returns true if this object has a datetime greater than $datetime. $datetime can be specified as
00414      a timestamp value or as an eZDateTime, eZDate or eZTime object. If $equal is true it returns true if
00415      they are equal as well.
00416      \note If $datetime is either eZDate or eZTime it will create temporary objects with toDate() and
00417      toTime() and use these for comparison.
00418     */
00419     function isGreaterThan( &$datetime, $equal = false )
00420     {
00421         if ( $datetime instanceof eZDate )
00422         {
00423             $d1 = $this->toDate();
00424             return $d1->isGreaterThan( $datetime, $equal );
00425         }
00426         else if ( $datetime instanceof eZTime )
00427         {
00428             $t1 = $this->toTime();
00429             return $t1->isGreaterThan( $datetime, $equal );
00430         }
00431         else
00432         {
00433             $dt1 = $this->timeStamp();
00434             $dt2 = $datetime instanceof eZDateTime ? $datetime->timeStamp() : $datetime;
00435 
00436             if ( $dt1 > $dt2 )
00437                 return true;
00438             else if ( $equal and $dt1 == $dt2 )
00439                 return true;
00440             else
00441                 return false;
00442         }
00443     }
00444     /*!
00445      Returns true if this object is equal to $date. $date can be specified as
00446      a timestamp value or as an eZDateTime, eZDate or eZTime object.
00447      \note If $datetime is either eZDate or eZTime it will create temporary objects with toDate() and
00448      toTime() and use these for comparison.
00449     */
00450     function isEqualTo( &$datetime )
00451     {
00452         if ( $datetime instanceof eZDate )
00453         {
00454             $d1 = $this->toDate();
00455             return $d1->isEqualTo( $datetime );
00456         }
00457         else if ( $datetime instanceof eZTime )
00458         {
00459             $t1 = $this->toTime();
00460             return $t1->isEqualTo( $datetime );
00461         }
00462         else
00463         {
00464             $dt1 = $this->timeStamp();
00465             $dt2 = $datetime instanceof eZDateTime ? $datetime->timeStamp() : $datetime;
00466 
00467             return $dt1 == $dt2;
00468         }
00469     }
00470 
00471     /*!
00472      Creates a new eZDate object with the time values $hour, $minute and $second,
00473      date values $month, $day and $year and returns a reference to it.
00474      Any value can be ommitted or set to -1 to use the current date or time value.
00475     */
00476     static function create( $hour = -1, $minute = -1, $second = -1, $month = -1, $day = -1, $year = -1 )
00477     {
00478         if ( $year != -1 )
00479             $datetime = mktime( $hour, $minute, $second, $month, $day, $year );
00480         else if ( $day != -1 )
00481             $datetime = mktime( $hour, $minute, $second, $month, $day );
00482         else if ( $month != -1 )
00483             $datetime = mktime( $hour, $minute, $second, $month );
00484         else if ( $second != -1 )
00485             $datetime = mktime( $hour, $minute, $second );
00486         else if ( $minute != -1 )
00487             $datetime = mktime( $hour, $minute );
00488         else if ( $hour != -1 )
00489             $datetime = mktime( $hour );
00490         else
00491             $datetime = time();
00492         return new eZDateTime( $datetime );
00493     }
00494 
00495     /*!
00496      \deprecated This function is deprecated in PHP5, use the PHP5 clone keyword instead
00497      Creates an exact copy of this object and returns it.
00498     */
00499     function duplicate()
00500     {
00501         $copy = clone $this;
00502         return $copy;
00503     }
00504 
00505     /*!
00506      Creates a string representation of the date using the current locale and returns it.
00507      If $short is true a short representation is used.
00508     */
00509     function toString( $short = false )
00510     {
00511         if ( $short )
00512             $str = $this->Locale->formatShortDate( $this->DateTime ) . ' ' .
00513                 $this->Locale->formatShortTime( $this->DateTime );
00514         else
00515             $str = $this->Locale->formatDate( $this->DateTime ) . ' ' .
00516                 $this->Locale->formatTime( $this->DateTime );
00517         return $str;
00518     }
00519 
00520     /// Locale object, is just a reference to minimize memory usage.
00521     public $Locale;
00522     /// The current datetime as a timestamp
00523     public $DateTime;
00524     public $IsValid;
00525 }
00526 
00527 ?>