ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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
92 public function isNull()
93 {
94 return $this->unix ? false : true;
95 }
96
104 public function switchTimeZone($a_timezone_identifier = '')
105 {
106 try
107 {
108 $this->timezone = ilTimeZone::_getInstance($a_timezone_identifier);
109 return true;
110 }
111 catch(ilTimeZoneException $e)
112 {
113 $this->log->write('Unsupported timezone given: '.$a_timezone_identifier);
114 throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_timezone_identifier);
115 }
116 }
117
124 public function getTimeZoneIdentifier()
125 {
126 return $this->timezone->getIdentifier();
127 }
128
143 public static function _before(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
144 {
145 switch($a_compare_field)
146 {
147 case IL_CAL_YEAR:
148 return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
149
150 case IL_CAL_MONTH:
151 return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
152
153 case IL_CAL_DAY:
154 return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
155
156 case '':
157 default:
158 return $start->get(IL_CAL_UNIX) < $end->get(IL_CAL_UNIX);
159
160 }
161 }
162
174 public static function _equals(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
175 {
176 switch($a_compare_field)
177 {
178 case IL_CAL_YEAR:
179 return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
180
181 case IL_CAL_MONTH:
182 return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
183
184 case IL_CAL_DAY:
185 return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
186
187 case '':
188 default:
189 return $start->get(IL_CAL_UNIX) == $end->get(IL_CAL_UNIX);
190
191 }
192 }
193
206 public static function _after(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
207 {
208 switch($a_compare_field)
209 {
210 case IL_CAL_YEAR:
211 return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
212
213 case IL_CAL_MONTH:
214 return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
215
216 case IL_CAL_DAY:
217 return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
218
219 case '':
220 default:
221 return $start->get(IL_CAL_UNIX) > $end->get(IL_CAL_UNIX);
222
223 }
224 }
225
234 public static function _within(ilDateTime $dt, ilDateTime $start, ilDateTime $end, $a_compare_field = '', $a_tz = '')
235 {
236 return
237 (ilDateTime::_after($dt, $start,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $start,$a_compare_field,$a_tz)) &&
238 (ilDateTime::_before($dt, $end,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $end,$a_compare_field,$a_tz));
239 }
240
249 public function increment($a_type,$a_count = 1)
250 {
251 $count_str = $a_count > 0 ? ('+'.$a_count.' ') : ($a_count.' ');
252
253 $this->timezone->switchTZ();
254 switch($a_type)
255 {
256 case self::YEAR:
257 $this->unix = strtotime($count_str.'year',$this->unix);
258 break;
259
260 case self::MONTH:
261 $this->unix = strtotime($count_str.'month',$this->unix);
262 break;
263
264 case self::WEEK:
265 $this->unix = strtotime($count_str.'week',$this->unix);
266 break;
267
268 case self::DAY:
269 $this->unix = strtotime($count_str.'day',$this->unix);
270 break;
271
272 case self::HOUR:
273 $this->unix = strtotime($count_str.'hour',$this->unix);
274 break;
275
276 case self::MINUTE:
277
278 $this->unix = strtotime($count_str.'minute',$this->unix);
279 $d = new ilDateTime($this->unix,IL_CAL_UNIX);
280
281
282 break;
283
284 }
285 $this->timezone->restoreTZ();
286 return $this->unix;
287 }
288
295 public function getUnixTime()
296 {
297 return $this->unix;
298 }
299
300
307 public function getUTCOffset()
308 {
309 $this->timezone->switchTZ();
310 // TODO: This is wrong: calculate UTC offset of given date
311 $offset = mktime(0,0,0,2,1,1970) - gmmktime(0,0,0,2,1,1970);
312 $this->timezone->restoreTZ();
313 return $offset;
314 }
315
324 public function setDate($a_date,$a_format)
325 {
326 switch($a_format)
327 {
328 case IL_CAL_UNIX:
329 $this->unix = $a_date;
330 break;
331
332 case IL_CAL_DATETIME:
333 $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);
334 if($matches < 1)
335 {
336 $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
337 $this->log->write(__METHOD__.': '.print_r($matches,true));
338 $this->log->logStack();
339 throw new ilDateTimeException('Cannot parse date.');
340 }
341
342 // UTC designator
343 if($d_parts[9] == 'Z')
344 {
345 $utc = ilTimeZone::_getInstance('UTC');
346 $utc->switchTZ();
347 }
348 else
349 {
350 $this->timezone->switchTZ();
351 }
352 $this->unix = mktime(
353 isset($d_parts[5]) ? $d_parts[5] : 0,
354 isset($d_parts[6]) ? $d_parts[6] : 0,
355 isset($d_parts[7]) ? $d_parts[7] : 0,
356 $d_parts[2],
357 $d_parts[3],
358 $d_parts[1]);
359
360 if($d_parts[0] == '0000-00-00 00:00:00')
361 {
362 $this->unix = 0;
363 }
364
365 if($d_parts[9] == 'Z')
366 {
367 $utc->restoreTZ();
368 }
369 else
370 {
371 $this->timezone->restoreTZ();
372 }
373 break;
374
375 case IL_CAL_DATE:
376 // Pure dates are not timezone sensible.
378 $timezone->switchTZ();
379 $unix = strtotime($a_date);
380 $timezone->restoreTZ();
381 if($unix === false)
382 {
383 $this->log->write(__METHOD__.': Cannot parse date : '.$a_date);
384 $this->unix = 0;
385 return false;
386 }
387 $this->unix = $unix;
388 break;
389
391 if (!isset($a_date['seconds']))
392 {
393 $a_date['seconds'] = false;
394 }
395 // Format like getdate parameters
396 $this->timezone->switchTZ();
397 $this->unix = mktime(
398 $a_date['hours'],
399 $a_date['minutes'],
400 $a_date['seconds'],
401 $a_date['mon'],
402 $a_date['mday'],
403 $a_date['year']);
404 $this->timezone->restoreTZ();
405
406 // TODO: choose better error handling
407 if(!$a_date['year'])
408 {
409 $this->unix = 0;
410 }
411 break;
412
413 case IL_CAL_TIMESTAMP:
414 if(preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) == false)
415 {
416 $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
417 throw new ilDateTimeException('Cannot parse date.');
418 }
419 $this->timezone->switchTZ();
420 $this->unix = mktime(
421 isset($d_parts[4]) ? $d_parts[4] : 0,
422 isset($d_parts[5]) ? $d_parts[5] : 0,
423 isset($d_parts[6]) ? $d_parts[6] : 0,
424 $d_parts[2],
425 $d_parts[3],
426 $d_parts[1]);
427
428 if($d_parts[0] == '00000000000000' or
429 $d_parts[0] == '00000000')
430 {
431 $this->unix = 0;
432 }
433 $this->timezone->restoreTZ();
434 break;
435
436 case IL_CAL_ISO_8601:
437 $dt = DateTime::createFromFormat(DateTime::ISO8601, $a_date);
438 $this->unix = $dt->getTimeStamp();
439 break;
440
441 }
442 return true;
443 }
444
453 public function get($a_format,$a_format_str = '',$a_tz = '')
454 {
455 if($a_tz)
456 {
457 try
458 {
460 }
461 catch(ilTimeZoneException $exc)
462 {
463 $this->log->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_tz);
464 }
465 }
466 else
467 {
468 #$timezone = $this->timezone;
470 }
471
472 switch($a_format)
473 {
474 case IL_CAL_UNIX:
475 $date = $this->getUnixTime();
476 break;
477
478 case IL_CAL_DATE:
479 $timezone->switchTZ();
480 $date = date('Y-m-d',$this->getUnixTime());
481 $timezone->restoreTZ();
482 break;
483
484 case IL_CAL_DATETIME:
485 $timezone->switchTZ();
486 $date = date('Y-m-d H:i:s',$this->getUnixTime());
487 $timezone->restoreTZ();
488 break;
489
490 case IL_CAL_FKT_DATE:
491 $timezone->switchTZ();
492 $date = date($a_format_str,$this->getUnixTime());
493 $timezone->restoreTZ();
494 break;
495
497 $timezone->switchTZ();
498 $date = getdate($this->getUnixTime());
499 $timezone->restoreTZ();
500
501 // add iso 8601 week day number (Sunday = 7)
502 $date['isoday'] = $date['wday'] == 0 ? 7 : $date['wday'];
503 break;
504
505 case IL_CAL_ISO_8601:
506 $date = date('c',$this->getUnixTime());
507 break;
508
509 case IL_CAL_TIMESTAMP:
510 $timezone->switchTZ();
511 $date = date('YmdHis',$this->getUnixTime());
512 $timezone->restoreTZ();
513 break;
514 }
515 return $date;
516 }
517
526 public function __toString()
527 {
528 return $this->get(IL_CAL_DATETIME).'<br>';
529 }
530}
531?>
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