| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZDate class 4 // 5 // Created on: <01-Mar-2002 13:48:04 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 eZDate ezdate.php 31 \ingroup eZLocale 32 \brief Locale aware date handler 33 34 eZDate handles date values in months, days and years. 35 The time stored as a timestamp with 0 hours, 0 minutes and 0 seconds. 36 37 A new instance of eZDate will automaticly use the current locale and current date, 38 if you however want a different locale use the setLocale() function. The current locale can be 39 fetched with locale(). 40 41 Change the date directly with setYear(), setMonth(), setDay() and setMDY(). 42 You can also adjust the date relative to it's current value by using adjustDate(). 43 Use timeStamp() to get the current timestamp value or year(), month() and day() 44 for the respective values. 45 46 When creating new times you're advised to use the static create() 47 function which returns a new eZDate object. You can also create a copy 48 with the duplicate() function. 49 50 Date checking is done with the isGreaterThan() and isEqualTo() functions. 51 52 Text output is done with toString() which can return a long string (default) or 53 short string representation according to the current locale. 54 55 Example: 56 \code 57 include_once( 'lib/ezlocale/classes/ezlocale.php' ); 58 include_once( 'lib/ezlocale/classes/ezdate.php' ); 59 60 $us_locale =& eZLocale::instance( 'us' ); 61 62 $date1 = new eZDate(); 63 $date2 = eZDate::create(); 64 $date2->setLocale( $us_locale ); 65 $date2->adjustDate( 1, 2, 3 ); 66 $date3 =& $date1->duplicate(); 67 68 print( $date1->toString() ); 69 print( $date2->toString( true ) ); 70 print( $date1->isEqualTo( $date3 ) ? 'true' : 'false' ); // Prints 'true' 71 72 \endcode 73 74 \sa eZTime, eZDateTime, eZLocale 75 */ 76 77 include_once ( 'lib/ezlocale/classes/ezlocale.php' ); 78 79 class eZDate 80 { 81 /*! 82 Creates a new date object with default locale, if $date is not supplied 83 the current date is used. 84 */ 85 function eZDate( $date = false ) 86 { 87 if ( $date === false ) 88 { 89 $date = mktime( 0, 0, 0 ); 90 } 91 else 92 { 93 $arr = getdate( $date ); 94 $date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] ); 95 } 96 $this->Date = $date; 97 $this->Locale =& eZLocale::instance(); 98 $this->IsValid = $date > 0; 99 } 100 101 function attributes() 102 { 103 return array( 'timestamp', 104 'is_valid', 105 'year', 106 'month', 107 'day' ); 108 } 109 110 function hasAttribute( $name ) 111 { 112 return in_array( $name, $this->attributes() ); 113 } 114 115 function &attribute( $name ) 116 { 117 if ( $name == 'timestamp' ) 118 $retValue = $this->timeStamp(); 119 else if ( $name == 'is_valid' ) 120 $retValue = $this->isValid(); 121 else if ( $name == 'day' ) 122 $retValue = $this->day(); 123 else if ( $name == 'year' ) 124 $retValue = $this->year(); 125 else if ( $name == 'month' ) 126 $retValue = $this->month(); 127 else 128 { 129 eZDebug::writeError( "Attribute '$name' does not exist", 'eZDate::attribute' ); 130 $retValue = false; 131 } 132 return $retValue; 133 } 134 135 /*! 136 \return true if the date has valid data. 137 */ 138 function isValid() 139 { 140 return $this->IsValid; 141 } 142 143 /*! 144 Sets the locale to $locale which is used in text output. 145 */ 146 function setLocale( &$locale ) 147 { 148 $this->Locale =& $locale; 149 } 150 151 /*! 152 Returns a reference to the current locale. 153 */ 154 function &locale() 155 { 156 return $this->Locale; 157 } 158 159 /*! 160 Returns the timestamp value, this is the number of seconds since the epoch 161 with hours, minutes and seconds set to 0. 162 \note The value is returned as a reference and should not be modified. 163 */ 164 function timeStamp() 165 { 166 return $this->Date; 167 } 168 169 function setTimeStamp( $stamp ) 170 { 171 $this->Date = $stamp; 172 $this->IsValid = $stamp > 0; 173 } 174 175 /*! 176 Sets the year leaving the other elements untouched. 177 */ 178 function setYear( $year ) 179 { 180 $arr = getdate( $this->Date ); 181 $this->Day = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $year ); 182 } 183 184 /*! 185 Sets the month leaving the other elements untouched. 186 */ 187 function setMonth( $month ) 188 { 189 $arr = getdate( $this->Date ); 190 $this->Day = mktime( 0, 0, 0, $month, $arr['mday'], $arr['year'] ); 191 } 192 193 /*! 194 Sets the day leaving the other elements untouched. 195 */ 196 function setDay( $day ) 197 { 198 $arr = getdate( $this->Date ); 199 $this->Day = mktime( 0, 0, 0, $arr['mon'], $day, $arr['year'] ); 200 } 201 202 /*! 203 Returns the year element. 204 */ 205 function year() 206 { 207 return date( 'Y', $this->Date ); 208 } 209 210 /*! 211 Returns the month element. 212 */ 213 function month() 214 { 215 return date( 'm', $this->Date ); 216 } 217 218 /*! 219 Returns the day element. 220 */ 221 function day() 222 { 223 return date( 'd', $this->Date ); 224 } 225 226 /*! 227 Sets the year, month and day elements. If $day or $year is omitted or set 0 228 they will get a value taken from the current time. 229 */ 230 function setMDY( $month, $day = 0, $year = 0 ) 231 { 232 if ( $year != 0 ) 233 $date = mktime( 0, 0, 0, $month, $day, $year ); 234 else if ( $day != 0 ) 235 $date = mktime( 0, 0, 0, $month, $day ); 236 else 237 $date = mktime( 0, 0, 0, $month ); 238 $this->Date = $date; 239 } 240 241 /*! 242 Adjusts the date relative to it's current value. This is useful for adding/subtracting 243 years, months or days to an existing date. 244 */ 245 function adjustDate( $month, $day = 0, $year = 0 ) 246 { 247 $arr = getdate( $this->Date ); 248 $date = mktime( 0, 0, 0, $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] ); 249 $this->Date = $date; 250 } 251 252 /*! 253 Returns true if this object has a date greater than $date. $date can be specified as 254 a timestamp value or as an eZDate object. If $equal is true it returns true if 255 they are equal as well. 256 */ 257 function isGreaterThan( &$date, $equal = false ) 258 { 259 $d1 = $this->timeStamp(); 260 if ( get_class( $date ) == 'ezdate' ) 261 $d2 = $date->timeStamp(); 262 else 263 { 264 $arr = getdate( $date ); 265 $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] ); 266 } 267 if ( $d1 > $d2 ) 268 return true; 269 else if ( $equal and $d1 == $d2 ) 270 return true; 271 else 272 return false; 273 } 274 /*! 275 Returns true if this object is equal to $date. $date can be specified as 276 a timestamp value or as an eZDate object. 277 */ 278 function isEqualTo( &$date ) 279 { 280 $d1 = $this->timeStamp(); 281 if ( get_class( $date ) == 'ezdate' ) 282 $d2 = $date->timeStamp(); 283 else 284 { 285 $arr = getdate( $date ); 286 $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] ); 287 } 288 return $d1 == $d2; 289 } 290 291 /*! 292 Creates a new eZDate object with the date values $month, $day and $year and returns a reference to it. 293 Any value can be ommitted or set to 0 to use the current date value. 294 */ 295 function create( $month, $day = 0, $year = 0 ) 296 { 297 if ( $year != 0 ) 298 $date = mktime( 0, 0, 0, $month, $day, $year ); 299 else if ( $day != 0 ) 300 $date = mktime( 0, 0, 0, $month, $day ); 301 else 302 $date = mktime( 0, 0, 0, $month ); 303 $newDateObject = new eZDate( $date ); 304 return $newDateObject; 305 } 306 307 /*! 308 Creates an exact copy of this object and returns a reference to it. 309 */ 310 function &duplicate() 311 { 312 $d = new eZDate( $this->Date ); 313 $d->setLocale( $this->Locale ); 314 return $d; 315 } 316 317 /*! 318 Creates a string representation of the date using the current locale and returns it. 319 If $short is true a short representation is used. 320 */ 321 function toString( $short = false ) 322 { 323 if ( $short ) 324 $str = $this->Locale->formatShortDate( $this->Date ); 325 else 326 $str = $this->Locale->formatDate( $this->Date ); 327 return $str; 328 } 329 330 331 /// Locale object, is just a reference to minimize memory usage. 332 var $Locale; 333 /// The current date as a timestamp without hour, minute or second values 334 var $Date; 335 var $IsValid; 336 } 337 338 ?>
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 |