ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilDateTime.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once('Services/Calendar/classes/class.ilDateTimeException.php');
6include_once('Services/Calendar/classes/class.ilTimeZone.php');
7
8
9define('IL_CAL_DATETIME',1);
10define('IL_CAL_DATE',2);
11define('IL_CAL_UNIX',3);
12define('IL_CAL_FKT_DATE',4);
13define('IL_CAL_FKT_GETDATE',5);
14define('IL_CAL_TIMESTAMP',6);
15define('IL_CAL_ISO_8601',7);
16
17define('IL_CAL_YEAR','year');
18define('IL_CAL_MONTH','month');
19define('IL_CAL_WEEK','week');
20define('IL_CAL_DAY','day');
21define('IL_CAL_HOUR','hour');
22
23
33{
34 const YEAR = 'year';
35 const MONTH = 'month';
36 const WEEK = 'week';
37 const DAY = 'day';
38 const HOUR = 'hour';
39 const MINUTE = 'minute';
40
41 protected $log;
42
43 protected $timezone = null;
44 protected $default_timezone = null;
45
46 protected $unix = 0;
47
48
49
60 public function __construct($a_date = null,$a_format = 0,$a_tz = '')
61 {
62 global $ilLog;
63
64 $this->log = $ilLog;
65
66 try
67 {
68 $this->timezone = ilTimeZone::_getInstance($a_tz);
69 $this->default_timezone = ilTimeZone::_getInstance('');
70
71 if(!$a_date)
72 {
73 $this->setDate(0,IL_CAL_UNIX);
74 }
75 else
76 {
77 $this->setDate($a_date,$a_format);
78 }
79 }
80 catch(ilTimeZoneException $exc)
81 {
82 $this->log->write(__METHOD__.': '.$exc->getMessage());
83 throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_tz);
84 }
85 }
86
87 public function __sleep()
88 {
89 return array('timezone', 'default_timezone', 'unix');
90 }
91
92 public function __wakeup()
93 {
94 global $ilLog;
95
96 $this->log = $ilLog;
97 }
98
99
105 public function isNull()
106 {
107 return $this->unix ? false : true;
108 }
109
117 public function switchTimeZone($a_timezone_identifier = '')
118 {
119 try
120 {
121 $this->timezone = ilTimeZone::_getInstance($a_timezone_identifier);
122 return true;
123 }
124 catch(ilTimeZoneException $e)
125 {
126 $this->log->write('Unsupported timezone given: '.$a_timezone_identifier);
127 throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_timezone_identifier);
128 }
129 }
130
137 public function getTimeZoneIdentifier()
138 {
139 return $this->timezone->getIdentifier();
140 }
141
156 public static function _before(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
157 {
158 switch($a_compare_field)
159 {
160 case IL_CAL_YEAR:
161 return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
162
163 case IL_CAL_MONTH:
164 return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
165
166 case IL_CAL_DAY:
167 return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
168
169 case '':
170 default:
171 return $start->get(IL_CAL_UNIX) < $end->get(IL_CAL_UNIX);
172
173 }
174 }
175
187 public static function _equals(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
188 {
189 switch($a_compare_field)
190 {
191 case IL_CAL_YEAR:
192 return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
193
194 case IL_CAL_MONTH:
195 return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
196
197 case IL_CAL_DAY:
198 return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
199
200 case '':
201 default:
202 return $start->get(IL_CAL_UNIX) == $end->get(IL_CAL_UNIX);
203
204 }
205 }
206
219 public static function _after(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
220 {
221 switch($a_compare_field)
222 {
223 case IL_CAL_YEAR:
224 return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
225
226 case IL_CAL_MONTH:
227 return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
228
229 case IL_CAL_DAY:
230 return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
231
232 case '':
233 default:
234 return $start->get(IL_CAL_UNIX) > $end->get(IL_CAL_UNIX);
235
236 }
237 }
238
247 public static function _within(ilDateTime $dt, ilDateTime $start, ilDateTime $end, $a_compare_field = '', $a_tz = '')
248 {
249 return
250 (ilDateTime::_after($dt, $start,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $start,$a_compare_field,$a_tz)) &&
251 (ilDateTime::_before($dt, $end,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $end,$a_compare_field,$a_tz));
252 }
253
262 public function increment($a_type,$a_count = 1)
263 {
264 $count_str = $a_count > 0 ? ('+'.$a_count.' ') : ($a_count.' ');
265
266 $this->timezone->switchTZ();
267 switch($a_type)
268 {
269 case self::YEAR:
270 $this->unix = strtotime($count_str.'year',$this->unix);
271 break;
272
273 case self::MONTH:
274 $this->unix = strtotime($count_str.'month',$this->unix);
275 break;
276
277 case self::WEEK:
278 $this->unix = strtotime($count_str.'week',$this->unix);
279 break;
280
281 case self::DAY:
282 $this->unix = strtotime($count_str.'day',$this->unix);
283 break;
284
285 case self::HOUR:
286 $this->unix = strtotime($count_str.'hour',$this->unix);
287 break;
288
289 case self::MINUTE:
290
291 $this->unix = strtotime($count_str.'minute',$this->unix);
292 $d = new ilDateTime($this->unix,IL_CAL_UNIX);
293
294
295 break;
296
297 }
298 $this->timezone->restoreTZ();
299 return $this->unix;
300 }
301
308 public function getUnixTime()
309 {
310 return $this->unix;
311 }
312
313
320 public function getUTCOffset()
321 {
322 $this->timezone->switchTZ();
323 // TODO: This is wrong: calculate UTC offset of given date
324 $offset = mktime(0,0,0,2,1,1970) - gmmktime(0,0,0,2,1,1970);
325 $this->timezone->restoreTZ();
326 return $offset;
327 }
328
337 public function setDate($a_date,$a_format)
338 {
339 switch($a_format)
340 {
341 case IL_CAL_UNIX:
342 $this->unix = $a_date;
343 break;
344
345 case IL_CAL_DATETIME:
346 $matches = preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)$/i',$a_date,$d_parts);
347 if($matches < 1)
348 {
349 $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
350 $this->log->write(__METHOD__.': '.print_r($matches,true));
351 $this->log->logStack();
352 throw new ilDateTimeException('Cannot parse date.');
353 }
354
355 // UTC designator
356 if($d_parts[9] == 'Z')
357 {
358 $utc = ilTimeZone::_getInstance('UTC');
359 $utc->switchTZ();
360 }
361 else
362 {
363 $this->timezone->switchTZ();
364 }
365 $this->unix = mktime(
366 isset($d_parts[5]) ? $d_parts[5] : 0,
367 isset($d_parts[6]) ? $d_parts[6] : 0,
368 isset($d_parts[7]) ? $d_parts[7] : 0,
369 $d_parts[2],
370 $d_parts[3],
371 $d_parts[1]);
372
373 if($d_parts[0] == '0000-00-00 00:00:00')
374 {
375 $this->unix = 0;
376 }
377
378 if($d_parts[9] == 'Z')
379 {
380 $utc->restoreTZ();
381 }
382 else
383 {
384 $this->timezone->restoreTZ();
385 }
386 break;
387
388 case IL_CAL_DATE:
389 // Pure dates are not timezone sensible.
391 $timezone->switchTZ();
392 $unix = strtotime($a_date);
393 $timezone->restoreTZ();
394 if($unix === false)
395 {
396 $this->log->write(__METHOD__.': Cannot parse date : '.$a_date);
397 $this->unix = 0;
398 return false;
399 }
400 $this->unix = $unix;
401 break;
402
404 if (!isset($a_date['seconds']))
405 {
406 $a_date['seconds'] = false;
407 }
408 // Format like getdate parameters
409 $this->timezone->switchTZ();
410 $this->unix = mktime(
411 $a_date['hours'],
412 $a_date['minutes'],
413 $a_date['seconds'],
414 $a_date['mon'],
415 $a_date['mday'],
416 $a_date['year']);
417 $this->timezone->restoreTZ();
418
419 // TODO: choose better error handling
420 if(!$a_date['year'])
421 {
422 $this->unix = 0;
423 }
424 break;
425
426 case IL_CAL_TIMESTAMP:
427 if(preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) == false)
428 {
429 $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
430 throw new ilDateTimeException('Cannot parse date.');
431 }
432 $this->timezone->switchTZ();
433 $this->unix = mktime(
434 isset($d_parts[4]) ? $d_parts[4] : 0,
435 isset($d_parts[5]) ? $d_parts[5] : 0,
436 isset($d_parts[6]) ? $d_parts[6] : 0,
437 $d_parts[2],
438 $d_parts[3],
439 $d_parts[1]);
440
441 if($d_parts[0] == '00000000000000' or
442 $d_parts[0] == '00000000')
443 {
444 $this->unix = 0;
445 }
446 $this->timezone->restoreTZ();
447 break;
448
449 case IL_CAL_ISO_8601:
450 $dt = DateTime::createFromFormat(DateTime::ISO8601, $a_date);
451 $this->unix = $dt->getTimeStamp();
452 break;
453
454 }
455 return true;
456 }
457
466 public function get($a_format,$a_format_str = '',$a_tz = '')
467 {
468 if($a_tz)
469 {
470 try
471 {
473 }
474 catch(ilTimeZoneException $exc)
475 {
476 $this->log->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_tz);
477 }
478 }
479 else
480 {
481 #$timezone = $this->timezone;
483 }
484
485 switch($a_format)
486 {
487 case IL_CAL_UNIX:
488 $date = $this->getUnixTime();
489 break;
490
491 case IL_CAL_DATE:
492 $timezone->switchTZ();
493 $date = date('Y-m-d',$this->getUnixTime());
494 $timezone->restoreTZ();
495 break;
496
497 case IL_CAL_DATETIME:
498 $timezone->switchTZ();
499 $date = date('Y-m-d H:i:s',$this->getUnixTime());
500 $timezone->restoreTZ();
501 break;
502
503 case IL_CAL_FKT_DATE:
504 $timezone->switchTZ();
505 $date = date($a_format_str,$this->getUnixTime());
506 $timezone->restoreTZ();
507 break;
508
510 $timezone->switchTZ();
511 $date = getdate($this->getUnixTime());
512 $timezone->restoreTZ();
513
514 // add iso 8601 week day number (Sunday = 7)
515 $date['isoday'] = $date['wday'] == 0 ? 7 : $date['wday'];
516 break;
517
518 case IL_CAL_ISO_8601:
519 $date = date('c',$this->getUnixTime());
520 break;
521
522 case IL_CAL_TIMESTAMP:
523 $timezone->switchTZ();
524 $date = date('YmdHis',$this->getUnixTime());
525 $timezone->restoreTZ();
526 break;
527 }
528 return $date;
529 }
530
539 public function __toString()
540 {
541 return $this->get(IL_CAL_DATETIME).'<br>';
542 }
543}
544?>
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
const IL_CAL_ISO_8601
const IL_CAL_TIMESTAMP
const IL_CAL_UNIX
const IL_CAL_YEAR
const IL_CAL_DATETIME
const IL_CAL_MONTH
const IL_CAL_FKT_DATE
const IL_CAL_DAY
Class for DateTime exceptions.
@classDescription Date and time handling
static _equals(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
Check if two date are equal.
switchTimeZone($a_timezone_identifier='')
Switch timezone.
static _after(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
get($a_format, $a_format_str='', $a_tz='')
get formatted date
isNull()
Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)
__construct($a_date=null, $a_format=0, $a_tz='')
Create new date object.
getTimeZoneIdentifier()
get timezone identifier
getUTCOffset()
get UTC offset
__toString()
to string for date time objects Output is user time zone
getUnixTime()
get unix time
static _within(ilDateTime $dt, ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
Check whether an date is within a date duration given by start and end.
increment($a_type, $a_count=1)
increment
static _before(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
setDate($a_date, $a_format)
set date
Class for TimeZone exceptions.
static _getInstance($a_tz='')
get instance by timezone