Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdatetime.php
Go to the documentation of this file.
1 <?php
61 class eZDateTime
62 {
68 public function __construct( $datetime = false )
69 {
70 if ( $datetime instanceof eZDate )
71 {
72 $arr = getdate( $datetime->timeStamp() );
73 $arr2 = getdate( $this->DateTime );
74 $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'],
75 $arr['mon'], $arr['mday'], $arr['year'] );
76 }
77 else if ( $datetime instanceof eZTime )
78 {
79 $arr2 = getdate( $datetime->timeStamp() );
80 $arr = getdate( $this->DateTime );
81 $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'],
82 $arr['mon'], $arr['mday'], $arr['year'] );
83 }
84 else if ( $datetime === false )
85 {
86 $datetime = time();
87 }
88
89 $this->DateTime = intval( $datetime );
90 $this->Locale = eZLocale::instance();
91 $this->IsValid = $datetime !== null;
92 }
93
94 function attributes()
95 {
96 return array( 'timestamp',
97 'hour',
98 'minute',
99 'second',
100 'year',
101 'month',
102 'day',
103 'is_valid' );
104 }
105
106 function hasAttribute( $name )
107 {
108 return in_array( $name, $this->attributes() );
109 }
110
111 function attribute( $name )
112 {
113 if ( $name == 'timestamp' )
114 {
115 return $this->timeStamp();
116 }
117 else if ( $name == 'hour' )
118 {
119 return $this->hour();
120 }
121 else if ( $name == 'minute' )
122 {
123 return $this->minute();
124 }
125 else if ( $name == 'second' )
126 {
127 return $this->second();
128 }
129 else if ( $name == 'day' )
130 {
131 return $this->day();
132 }
133 else if ( $name == 'year' )
134 {
135 return $this->year();
136 }
137 else if ( $name == 'month' )
138 {
139 return $this->month();
140 }
141 else if ( $name == 'is_valid' )
142 {
143 return $this->isValid();
144 }
145 else
146 {
147 eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
148 return false;
149 }
150 }
151
155 function isValid()
156 {
157 return $this->IsValid;
158 }
159
163 function setLocale( $locale )
164 {
165 $this->Locale = $locale;
166 }
167
171 function locale()
172 {
173 return $this->Locale;
174 }
175
179 function timeZone()
180 {
181 return date( 'T', $this->DateTime );
182 }
183
188 function timeStamp( )
189 {
190 return $this->DateTime;
191 }
192
193 function setTimeStamp( $stamp )
194 {
195 $this->DateTime = $stamp;
196 $this->IsValid = $stamp !== null;
197 }
198
203 static function currentTimeStamp()
204 {
205 return time();
206 }
207
212 function toDate()
213 {
214 $date = new eZDate( $this->DateTime );
215 $date->setLocale( $this->Locale );
216 return $date;
217 }
218
223 function toTime()
224 {
225 $time = new eZTime( $this->DateTime );
226 $time->setLocale( $this->Locale );
227 return $time;
228 }
229
233 function year()
234 {
235 return date( 'Y', $this->DateTime );
236 }
237
241 function month()
242 {
243 return date( 'm', $this->DateTime );
244 }
245
249 function day()
250 {
251 return date( 'd', $this->DateTime );
252 }
253
257 function hour()
258 {
259 return date( 'G', $this->DateTime );
260 }
261
265 function minute()
266 {
267 return date( 'i', $this->DateTime );
268 }
269
273 function second()
274 {
275 return date( 's', $this->DateTime );
276 }
277
281 function setYear( $year )
282 {
283 $arr = getdate( $this->DateTime );
284 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
285 $arr['mon'], $arr['mday'], $year );
286 }
287
291 function setMonth( $month )
292 {
293 $arr = getdate( $this->DateTime );
294 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
295 $month, $arr['mday'], $arr['year'] );
296 }
297
301 function setDay( $day )
302 {
303 $arr = getdate( $this->DateTime );
304 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
305 $arr['mon'], $day, $arr['year'] );
306 }
307
311 function setHour( $hour )
312 {
313 $arr = getdate( $this->DateTime );
314 $this->DateTime = mktime( $hour, $arr['minutes'], $arr['seconds'],
315 $arr['mon'], $arr['mday'], $arr['year'] );
316 }
317
321 function setMinute( $min )
322 {
323 $arr = getdate( $this->DateTime );
324 $this->DateTime = mktime( $arr['hours'], $min, $arr['seconds'],
325 $arr['mon'], $arr['mday'], $arr['year'] );
326 }
327
331 function setSecond( $sec )
332 {
333 $arr = getdate( $this->DateTime );
334 $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $sec,
335 $arr['mon'], $arr['mday'], $arr['year'] );
336 }
337
341 function setHMS( $hour, $min = 0, $sec = 0 )
342 {
343 $arr = getdate( $this->DateTime );
344 $this->DateTime = mktime( $hour, $min, $sec,
345 $arr['mon'], $arr['mday'], $arr['year'] );
346 }
347
351 function setMDYHMS( $month, $day, $year, $hour, $min, $sec = 0 )
352 {
353 $this->DateTime = mktime( $hour, $min, $sec, $month, $day, $year );
354 }
355
360 function setMDY( $month, $day = 0, $year = 0 )
361 {
362 $arr = getdate( $this->DateTime );
363 if ( $year != 0 )
364 $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
365 $month, $day, $year );
366 else if ( $day != 0 )
367 $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
368 $month, $day );
369 else
370 $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
371 $month );
372 $this->DateTime = $date;
373 }
374
379 function adjustDateTime( $hour, $minute = 0, $second = 0, $month = 0, $day = 0, $year = 0 )
380 {
381 $arr = getdate( $this->DateTime );
382 $date = mktime( $hour + $arr['hours'], $minute + $arr['minutes'], $second + $arr['seconds'],
383 $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] );
384 $this->DateTime = $date;
385 }
386
394 function isGreaterThan( &$datetime, $equal = false )
395 {
396 if ( $datetime instanceof eZDate )
397 {
398 $d1 = $this->toDate();
399 return $d1->isGreaterThan( $datetime, $equal );
400 }
401 else if ( $datetime instanceof eZTime )
402 {
403 $t1 = $this->toTime();
404 return $t1->isGreaterThan( $datetime, $equal );
405 }
406 else
407 {
408 $dt1 = $this->timeStamp();
409 $dt2 = $datetime instanceof eZDateTime ? $datetime->timeStamp() : $datetime;
410
411 if ( $dt1 > $dt2 )
412 return true;
413 else if ( $equal and $dt1 == $dt2 )
414 return true;
415 else
416 return false;
417 }
418 }
425 function isEqualTo( &$datetime )
426 {
427 if ( $datetime instanceof eZDate )
428 {
429 $d1 = $this->toDate();
430 return $d1->isEqualTo( $datetime );
431 }
432 else if ( $datetime instanceof eZTime )
433 {
434 $t1 = $this->toTime();
435 return $t1->isEqualTo( $datetime );
436 }
437 else
438 {
439 $dt1 = $this->timeStamp();
440 $dt2 = $datetime instanceof eZDateTime ? $datetime->timeStamp() : $datetime;
441
442 return $dt1 == $dt2;
443 }
444 }
445
451 static function create( $hour = -1, $minute = -1, $second = -1, $month = -1, $day = -1, $year = -1 )
452 {
453 if ( $year != -1 )
454 $datetime = mktime( $hour, $minute, $second, $month, $day, $year );
455 else if ( $day != -1 )
456 $datetime = mktime( $hour, $minute, $second, $month, $day );
457 else if ( $month != -1 )
458 $datetime = mktime( $hour, $minute, $second, $month );
459 else if ( $second != -1 )
460 $datetime = mktime( $hour, $minute, $second );
461 else if ( $minute != -1 )
462 $datetime = mktime( $hour, $minute );
463 else if ( $hour != -1 )
464 $datetime = mktime( $hour );
465 else
466 $datetime = time();
467 return new eZDateTime( $datetime );
468 }
469
474 function duplicate()
475 {
476 $copy = clone $this;
477 return $copy;
478 }
479
484 function toString( $short = false )
485 {
486 if ( $short )
487 $str = $this->Locale->formatShortDate( $this->DateTime ) . ' ' .
488 $this->Locale->formatShortTime( $this->DateTime );
489 else
490 $str = $this->Locale->formatDate( $this->DateTime ) . ' ' .
491 $this->Locale->formatTime( $this->DateTime );
492 return $str;
493 }
494
496 public $Locale;
498 public $DateTime;
499 public $IsValid;
500 }
501
502 ?>
eZDateTime
Locale aware date and time handler.
Definition: ezdatetime.php:62
eZDateTime\toDate
toDate()
Definition: ezdatetime.php:212
eZDateTime\setMDY
setMDY( $month, $day=0, $year=0)
Definition: ezdatetime.php:360
eZDateTime\setYear
setYear( $year)
Definition: ezdatetime.php:281
eZDateTime\second
second()
Definition: ezdatetime.php:273
eZDateTime\month
month()
Definition: ezdatetime.php:241
eZDateTime\setMinute
setMinute( $min)
Definition: ezdatetime.php:321
eZDateTime\locale
locale()
Definition: ezdatetime.php:171
eZDateTime\setMDYHMS
setMDYHMS( $month, $day, $year, $hour, $min, $sec=0)
Definition: ezdatetime.php:351
eZDateTime\day
day()
Definition: ezdatetime.php:249
eZDateTime\year
year()
Definition: ezdatetime.php:233
eZDateTime\$DateTime
$DateTime
The current datetime as a timestamp.
Definition: ezdatetime.php:498
eZDateTime\hasAttribute
hasAttribute( $name)
Definition: ezdatetime.php:106
eZDateTime\create
static create( $hour=-1, $minute=-1, $second=-1, $month=-1, $day=-1, $year=-1)
Definition: ezdatetime.php:451
eZDateTime\setSecond
setSecond( $sec)
Definition: ezdatetime.php:331
eZDateTime\$Locale
$Locale
Locale object, is just a reference to minimize memory usage.
Definition: ezdatetime.php:496
eZDateTime\minute
minute()
Definition: ezdatetime.php:265
eZDateTime\setHour
setHour( $hour)
Definition: ezdatetime.php:311
eZDateTime\duplicate
duplicate()
Definition: ezdatetime.php:474
eZDateTime\hour
hour()
Definition: ezdatetime.php:257
eZDateTime\attributes
attributes()
Definition: ezdatetime.php:94
eZDateTime\toTime
toTime()
Definition: ezdatetime.php:223
eZDateTime\adjustDateTime
adjustDateTime( $hour, $minute=0, $second=0, $month=0, $day=0, $year=0)
Definition: ezdatetime.php:379
eZDateTime\currentTimeStamp
static currentTimeStamp()
Definition: ezdatetime.php:203
eZDateTime\toString
toString( $short=false)
Definition: ezdatetime.php:484
eZDateTime\setHMS
setHMS( $hour, $min=0, $sec=0)
Definition: ezdatetime.php:341
eZDateTime\setDay
setDay( $day)
Definition: ezdatetime.php:301
eZDateTime\setTimeStamp
setTimeStamp( $stamp)
Definition: ezdatetime.php:193
eZDateTime\isEqualTo
isEqualTo(&$datetime)
Definition: ezdatetime.php:425
eZDateTime\__construct
__construct( $datetime=false)
Definition: ezdatetime.php:68
eZDateTime\setLocale
setLocale( $locale)
Definition: ezdatetime.php:163
eZDateTime\$IsValid
$IsValid
Definition: ezdatetime.php:499
eZDateTime\timeStamp
timeStamp()
Definition: ezdatetime.php:188
eZDateTime\setMonth
setMonth( $month)
Definition: ezdatetime.php:291
eZDateTime\isValid
isValid()
Definition: ezdatetime.php:155
eZDateTime\attribute
attribute( $name)
Definition: ezdatetime.php:111
eZDateTime\timeZone
timeZone()
Definition: ezdatetime.php:179
eZDateTime\isGreaterThan
isGreaterThan(&$datetime, $equal=false)
Definition: ezdatetime.php:394
eZDate
Locale aware date handler.
Definition: ezdate.php:58
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
eZTime
Locale aware time handler.
Definition: eztime.php:59
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.