00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 include_once( 'lib/ezlocale/classes/ezlocale.php' );
00080
00081 class eZDate
00082 {
00083
00084
00085
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 $retValue = $this->timeStamp();
00121 else if ( $name == 'is_valid' )
00122 $retValue = $this->isValid();
00123 else if ( $name == 'day' )
00124 $retValue = $this->day();
00125 else if ( $name == 'year' )
00126 $retValue = $this->year();
00127 else if ( $name == 'month' )
00128 $retValue = $this->month();
00129 else
00130 {
00131 eZDebug::writeError( "Attribute '$name' does not exist", 'eZDate::attribute' );
00132 $retValue = false;
00133 }
00134 return $retValue;
00135 }
00136
00137
00138
00139
00140 function isValid()
00141 {
00142 return $this->IsValid;
00143 }
00144
00145
00146
00147
00148 function setLocale( &$locale )
00149 {
00150 $this->Locale =& $locale;
00151 }
00152
00153
00154
00155
00156 function &locale()
00157 {
00158 return $this->Locale;
00159 }
00160
00161
00162
00163
00164
00165
00166 function timeStamp()
00167 {
00168 return $this->Date;
00169 }
00170
00171 function setTimeStamp( $stamp )
00172 {
00173 $this->Date = $stamp;
00174 $this->IsValid = $stamp > 0;
00175 }
00176
00177
00178
00179
00180 function setYear( $year )
00181 {
00182 $arr = getdate( $this->Date );
00183 $this->Date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $year );
00184 }
00185
00186
00187
00188
00189 function setMonth( $month )
00190 {
00191 $arr = getdate( $this->Date );
00192 $this->Date = mktime( 0, 0, 0, $month, $arr['mday'], $arr['year'] );
00193 }
00194
00195
00196
00197
00198 function setDay( $day )
00199 {
00200 $arr = getdate( $this->Date );
00201 $this->Date = mktime( 0, 0, 0, $arr['mon'], $day, $arr['year'] );
00202 }
00203
00204
00205
00206
00207 function year()
00208 {
00209 return date( 'Y', $this->Date );
00210 }
00211
00212
00213
00214
00215 function month()
00216 {
00217 return date( 'm', $this->Date );
00218 }
00219
00220
00221
00222
00223 function day()
00224 {
00225 return date( 'd', $this->Date );
00226 }
00227
00228
00229
00230
00231
00232 function setMDY( $month, $day = 0, $year = 0 )
00233 {
00234 if ( $year != 0 )
00235 $date = mktime( 0, 0, 0, $month, $day, $year );
00236 else if ( $day != 0 )
00237 $date = mktime( 0, 0, 0, $month, $day );
00238 else
00239 $date = mktime( 0, 0, 0, $month );
00240 $this->Date = $date;
00241 }
00242
00243
00244
00245
00246
00247 function adjustDate( $month, $day = 0, $year = 0 )
00248 {
00249 $arr = getdate( $this->Date );
00250 $date = mktime( 0, 0, 0, $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] );
00251 $this->Date = $date;
00252 }
00253
00254
00255
00256
00257
00258
00259 function isGreaterThan( &$date, $equal = false )
00260 {
00261 $d1 = $this->timeStamp();
00262 if ( get_class( $date ) == 'ezdate' )
00263 $d2 = $date->timeStamp();
00264 else
00265 {
00266 $arr = getdate( $date );
00267 $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
00268 }
00269 if ( $d1 > $d2 )
00270 return true;
00271 else if ( $equal and $d1 == $d2 )
00272 return true;
00273 else
00274 return false;
00275 }
00276
00277
00278
00279
00280 function isEqualTo( &$date )
00281 {
00282 $d1 = $this->timeStamp();
00283 if ( get_class( $date ) == 'ezdate' )
00284 $d2 = $date->timeStamp();
00285 else
00286 {
00287 $arr = getdate( $date );
00288 $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
00289 }
00290 return $d1 == $d2;
00291 }
00292
00293
00294
00295
00296
00297 function create( $month, $day = 0, $year = 0 )
00298 {
00299 if ( $year != 0 )
00300 $date = mktime( 0, 0, 0, $month, $day, $year );
00301 else if ( $day != 0 )
00302 $date = mktime( 0, 0, 0, $month, $day );
00303 else
00304 $date = mktime( 0, 0, 0, $month );
00305 $newDateObject = new eZDate( $date );
00306 return $newDateObject;
00307 }
00308
00309
00310
00311
00312 function &duplicate()
00313 {
00314 $d = new eZDate( $this->Date );
00315 $d->setLocale( $this->Locale );
00316 return $d;
00317 }
00318
00319
00320
00321
00322
00323 function toString( $short = false )
00324 {
00325 if ( $short )
00326 $str = $this->Locale->formatShortDate( $this->Date );
00327 else
00328 $str = $this->Locale->formatDate( $this->Date );
00329 return $str;
00330 }
00331
00332
00333
00334 var $Locale;
00335
00336 var $Date;
00337 var $IsValid;
00338 }
00339
00340 ?>