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