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