[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZDateTime class 4 // 5 // Created on: <01-Mar-2002 13:48:12 amos> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZDateTime ezdatetime.php 31 \ingroup eZLocale 32 \brief Locale aware date and time handler 33 34 eZDateTime handles 24 hour time values in hours, minutes and seconds 35 and date values. 36 The datetime stored as a timestamp with the number of seconds since the epoch. 37 See PHP function date() and mktime() for more information. 38 39 A new instance of eZDateTime will automaticly use the current locale and current datetime, 40 if you however want a different locale use the setLocale() function. The current locale can be 41 fetched with locale(). 42 43 Change the time directly with setHour(), setMinute(), setSecond() and setHMS(). 44 Change the date directly with setYear(), setMonth(), setDay() and setMDY(). 45 You can also adjust the date time relative to it's current value by using 46 adjustDateTime(). Use timeStamp() to get the current timestamp value or 47 year(), month(), day(), hour(), minute() and second() for the respective 48 values. 49 50 When creating new datetimes you're advised to use the static create() 51 function which returns a new eZDateTime object. You can also create a copy 52 with the duplicate() function. 53 54 Time checking is done with the isGreaterThan() and isEqualTo() functions. 55 56 Text output is done with toString() which can return a long string (default) or 57 short string representation according to the current locale. 58 59 Example: 60 \code 61 include_once( 'lib/ezlocale/classes/ezlocale.php' ); 62 include_once( 'lib/ezlocale/classes/ezdatetime.php' ); 63 64 $us_locale =& eZLocale::instance( 'us' ); 65 66 $dt1 = new eZDateTime(); 67 $dt2 = eZDateTime::create(); 68 $dt2->setLocale( $us_locale ); 69 $dt2->adjustDateTime( -8, 0, 0, 1, 2, 3 ); 70 $dt3 = $dt1->duplicate(); 71 72 print( $dt1->toString() ); 73 print( $dt2->toString( true ) ); 74 print( $dt1->isEqualTo( $dt3 ) ? 'true' : 'false' ); // Prints 'true' 75 76 \endcode 77 78 \sa eZDate, eZTime, eZLocale 79 */ 80 81 include_once ( 'lib/ezlocale/classes/ezlocale.php' ); 82 include_once ( 'lib/ezlocale/classes/ezdate.php' ); 83 include_once ( 'lib/ezlocale/classes/eztime.php' ); 84 85 class eZDateTime 86 { 87 /*! 88 Creates a new datetime object with default locale, if $datetime is not supplied 89 the current datetime is used. 90 */ 91 function eZDateTime( $datetime = false ) 92 { 93 if ( get_class( $datetime ) == 'ezdate' ) 94 { 95 $arr = getdate( $datetime->timeStamp() ); 96 $arr2 = getdate( $this->DateTime ); 97 $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'], 98 $arr['mon'], $arr['mday'], $arr['year'] ); 99 } 100 else if ( get_class( $datetime ) == 'eztime' ) 101 { 102 $arr2 = getdate( $datetime->timeStamp() ); 103 $arr = getdate( $this->DateTime ); 104 $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'], 105 $arr['mon'], $arr['mday'], $arr['year'] ); 106 } 107 else if ( $datetime === false ) 108 { 109 $datetime = mktime(); 110 } 111 else 112 { 113 $arr = getdate( $datetime ); 114 $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'], 115 $arr['mon'], $arr['mday'], $arr['year'] ); 116 } 117 $this->DateTime =& $datetime; 118 $this->Locale =& eZLocale::instance(); 119 $this->IsValid = $datetime > 0; 120 } 121 122 function attributes() 123 { 124 return array( 'timestamp', 125 'hour', 126 'minute', 127 'year', 128 'month', 129 'day', 130 'is_valid' ); 131 } 132 133 function hasAttribute( $name ) 134 { 135 return in_array( $name, $this->attributes() ); 136 } 137 138 function &attribute( $name ) 139 { 140 if ( $name == 'timestamp' ) 141 { 142 $ts = $this->timeStamp(); 143 return $ts; 144 } 145 else if ( $name == 'hour' ) 146 { 147 $hour = $this->hour(); 148 return $hour; 149 } 150 else if ( $name == 'minute' ) 151 { 152 $minute = $this->minute(); 153 return $minute; 154 } 155 else if ( $name == 'day' ) 156 { 157 $day = $this->day(); 158 return $day; 159 } 160 else if ( $name == 'year' ) 161 { 162 $year = $this->year(); 163 return $year; 164 } 165 else if ( $name == 'month' ) 166 { 167 $month = $this->month(); 168 return $month; 169 } 170 else if ( $name == 'is_valid' ) 171 { 172 $isValid = $this->isValid(); 173 return $isValid; 174 } 175 else 176 { 177 eZDebug::writeError( "Attribute '$name' does not exist", 'eZDateTime::attribute' ); 178 $retVal = false; 179 return $retVal; 180 } 181 } 182 183 /*! 184 \return true if the date has valid data. 185 */ 186 function isValid() 187 { 188 return $this->IsValid; 189 } 190 191 /*! 192 Sets the locale to $locale which is used in text output. 193 */ 194 function setLocale( &$locale ) 195 { 196 $this->Locale =& $locale; 197 } 198 199 /*! 200 Returns a reference to the current locale. 201 */ 202 function &locale() 203 { 204 return $this->Locale; 205 } 206 207 /*! 208 Returns the current time zone. 209 */ 210 function timeZone() 211 { 212 return date( 'T', $this->DateTime ); 213 } 214 215 /*! 216 Returns the timestamp value, this is the number of seconds since the epoch. 217 \note The value is returned as a reference and should not be modified. 218 */ 219 function timeStamp( ) 220 { 221 return $this->DateTime; 222 } 223 224 function setTimeStamp( $stamp ) 225 { 226 $this->DateTime = $stamp; 227 $this->IsValid = $stamp > 0; 228 } 229 230 /*! 231 \static 232 Returns the current date and time as a UNIX timestamp 233 */ 234 function currentTimeStamp() 235 { 236 return time(); 237 } 238 239 /*! 240 Creates an eZDate object of this datetime with the same date and locale. 241 Returns a reference to the object. 242 */ 243 function toDate() 244 { 245 $date = new eZDate( $this->DateTime ); 246 $date->setLocale( $this->Locale ); 247 return $date; 248 } 249 250 /*! 251 Creates an eZTime object of this datetime with the same time and locale. 252 Returns a reference to the object. 253 */ 254 function toTime() 255 { 256 $time = new eZTime( $this->DateTime ); 257 $time->setLocale( $this->Locale ); 258 return $time; 259 } 260 261 /*! 262 Returns the year element. 263 */ 264 function year() 265 { 266 return date( 'Y', $this->DateTime ); 267 } 268 269 /*! 270 Returns the month element. 271 */ 272 function month() 273 { 274 return date( 'm', $this->DateTime ); 275 } 276 277 /*! 278 Returns the day element. 279 */ 280 function day() 281 { 282 return date( 'd', $this->DateTime ); 283 } 284 285 /*! 286 Returns the hour element. 287 */ 288 function hour() 289 { 290 return date( 'G', $this->DateTime ); 291 } 292 293 /*! 294 Returns the minute element. 295 */ 296 function minute() 297 { 298 return date( 'i', $this->DateTime ); 299 } 300 301 /*! 302 Returns the second element. 303 */ 304 function second() 305 { 306 return date( 's', $this->DateTime ); 307 } 308 309 /*! 310 Sets the year leaving the other elements untouched. 311 */ 312 function setYear( $year ) 313 { 314 $arr = getdate( $this->DateTime ); 315 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'], 316 $arr['mon'], $arr['mday'], $year ); 317 } 318 319 /*! 320 Sets the month leaving the other elements untouched. 321 */ 322 function setMonth( $month ) 323 { 324 $arr = getdate( $this->DateTime ); 325 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'], 326 $month, $arr['mday'], $arr['year'] ); 327 } 328 329 /*! 330 Sets the day leaving the other elements untouched. 331 */ 332 function setDay( $day ) 333 { 334 $arr = getdate( $this->DateTime ); 335 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'], 336 $arr['mon'], $day, $arr['year'] ); 337 } 338 339 /*! 340 Sets the hour leaving the other elements untouched. 341 */ 342 function setHour( $hour ) 343 { 344 $arr = getdate( $this->DateTime ); 345 $this->DateTime = mktime( $hour, $arr['minutes'], $arr['seconds'], 346 $arr['mon'], $arr['mday'], $arr['year'] ); 347 } 348 349 /*! 350 Sets the minute leaving the other elements untouched. 351 */ 352 function setMinute( $min ) 353 { 354 $arr = getdate( $this->DateTime ); 355 $this->DateTime = mktime( $arr['hours'], $min, $arr['seconds'], 356 $arr['mon'], $arr['mday'], $arr['year'] ); 357 } 358 359 /*! 360 Sets the second leaving the other elements untouched. 361 */ 362 function setSecond( $sec ) 363 { 364 $arr = getdate( $this->DateTime ); 365 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $sec, 366 $arr['mon'], $arr['mday'], $arr['year'] ); 367 } 368 369 /*! 370 Sets all hour, minute and second elements leaving the other elements untouched. 371 */ 372 function setHMS( $hour, $min = 0, $sec = 0 ) 373 { 374 $arr = getdate( $this->DateTime ); 375 $this->DateTime = mktime( $hour, $min, $sec, 376 $arr['mon'], $arr['mday'], $arr['year'] ); 377 } 378 379 /*! 380 Sets all hour, minute and second elements leaving the other elements untouched. 381 */ 382 function setMDYHMS( $month, $day, $year, $hour, $min, $sec = 0 ) 383 { 384 $this->DateTime = mktime( $hour, $min, $sec, $month, $day, $year ); 385 } 386 387 /*! 388 Sets the year, month and day elements. If $day or $year is omitted or set 0 389 they will get a value taken from the current time. 390 */ 391 function setMDY( $month, $day = 0, $year = 0 ) 392 { 393 $arr = getdate( $this->DateTime ); 394 if ( $year != 0 ) 395 $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'], 396 $month, $day, $year ); 397 else if ( $day != 0 ) 398 $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'], 399 $month, $day ); 400 else 401 $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'], 402 $month ); 403 $this->DateTime =& $date; 404 } 405 406 /*! 407 Adjusts the datetime relative to it's current value. This is useful for adding/subtracting 408 hours, minutes, seconds, years, months or days to an existing datetime. 409 */ 410 function adjustDateTime( $hour, $minute = 0, $second = 0, $month = 0, $day = 0, $year = 0 ) 411 { 412 $arr = getdate( $this->DateTime ); 413 $date = mktime( $hour + $arr['hours'], $minute + $arr['minutes'], $second + $arr['seconds'], 414 $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] ); 415 $this->DateTime =& $date; 416 } 417 418 /*! 419 Returns true if this object has a datetime greater than $datetime. $datetime can be specified as 420 a timestamp value or as an eZDateTime, eZDate or eZTime object. If $equal is true it returns true if 421 they are equal as well. 422 \note If $datetime is either eZDate or eZTime it will create temporary objects with toDate() and 423 toTime() and use these for comparison. 424 */ 425 function isGreaterThan( &$datetime, $equal = false ) 426 { 427 if ( get_class( $datetime ) == 'ezdate' ) 428 { 429 $d1 = $this->toDate(); 430 return $d1->isGreaterThan( $datetime, $equal ); 431 } 432 else if ( get_class( $datetime ) == 'eztime' ) 433 { 434 $t1 = $this->toTime(); 435 return $t1->isGreaterThan( $datetime, $equal ); 436 } 437 else 438 { 439 $dt1 = $this->timeStamp(); 440 if ( get_class( $datetime ) == 'ezdatetime' ) 441 $dt2 = $datetime->timeStamp(); 442 else 443 $dt2 =& $datetime; 444 if ( $dt1 > $dt2 ) 445 return true; 446 else if ( $equal and $dt1 == $dt2 ) 447 return true; 448 else 449 return false; 450 } 451 } 452 /*! 453 Returns true if this object is equal to $date. $date can be specified as 454 a timestamp value or as an eZDateTime, eZDate or eZTime object. 455 \note If $datetime is either eZDate or eZTime it will create temporary objects with toDate() and 456 toTime() and use these for comparison. 457 */ 458 function isEqualTo( &$datetime ) 459 { 460 if ( get_class( $datetime ) == 'ezdate' ) 461 { 462 $d1 = $this->toDate(); 463 return $d1->isEqualTo( $datetime ); 464 } 465 else if ( get_class( $datetime ) == 'eztime' ) 466 { 467 $t1 = $this->toTime(); 468 return $t1->isEqualTo( $datetime ); 469 } 470 else 471 { 472 $dt1 = $this->timeStamp(); 473 if ( get_class( $datetime ) == 'ezdatetime' ) 474 $dt2 = $datetime->timeStamp(); 475 else 476 $dt2 =& $datetime; 477 return $dt1 == $dt2; 478 } 479 } 480 481 /*! 482 Creates a new eZDate object with the time values $hour, $minute and $second, 483 date values $month, $day and $year and returns a reference to it. 484 Any value can be ommitted or set to -1 to use the current date or time value. 485 */ 486 function create( $hour = -1, $minute = -1, $second = -1, $month = -1, $day = -1, $year = -1 ) 487 { 488 if ( $year != -1 ) 489 $datetime = mktime( $hour, $minute, $second, $month, $day, $year ); 490 else if ( $day != -1 ) 491 $datetime = mktime( $hour, $minute, $second, $month, $day ); 492 else if ( $month != -1 ) 493 $datetime = mktime( $hour, $minute, $second, $month ); 494 else if ( $second != -1 ) 495 $datetime = mktime( $hour, $minute, $second ); 496 else if ( $minute != -1 ) 497 $datetime = mktime( $hour, $minute ); 498 else if ( $hour != -1 ) 499 $datetime = mktime( $hour ); 500 else 501 $datetime = mktime(); 502 return new eZDateTime( $datetime ); 503 } 504 505 /*! 506 Creates an exact copy of this object and returns a reference to it. 507 */ 508 function &duplicate() 509 { 510 $dt = new eZDateTime( $this->DateTime ); 511 $dt->setLocale( $this->Locale ); 512 return $dt; 513 } 514 515 /*! 516 Creates a string representation of the date using the current locale and returns it. 517 If $short is true a short representation is used. 518 */ 519 function toString( $short = false ) 520 { 521 if ( $short ) 522 $str = $this->Locale->formatShortDate( $this->DateTime ) . ' ' . 523 $this->Locale->formatShortTime( $this->DateTime ); 524 else 525 $str = $this->Locale->formatDate( $this->DateTime ) . ' ' . 526 $this->Locale->formatTime( $this->DateTime ); 527 return $str; 528 } 529 530 /// Locale object, is just a reference to minimize memory usage. 531 var $Locale; 532 /// The current datetime as a timestamp 533 var $DateTime; 534 var $IsValid; 535 } 536 537 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |