ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
ilDateTime Class Reference

@classDescription Date and time handling More...

+ Inheritance diagram for ilDateTime:
+ Collaboration diagram for ilDateTime:

Public Member Functions

 __construct ($a_date=null, $a_format=0, $a_tz='')
 Create new date object. More...
 
 isNull ()
 Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...) More...
 
 switchTimeZone ($a_timezone_identifier='')
 Switch timezone. More...
 
 getTimeZoneIdentifier ()
 get timezone identifier More...
 
 increment ($a_type, $a_count=1)
 increment More...
 
 getUnixTime ()
 get unix time More...
 
 getUTCOffset ()
 get UTC offset More...
 
 setDate ($a_date, $a_format)
 set date More...
 
 get ($a_format, $a_format_str='', $a_tz='')
 get formatted date More...
 
 __toString ()
 to string for date time objects Output is user time zone More...
 

Static Public Member Functions

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. More...
 
static _equals (ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
 Check if two date are equal. More...
 
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. More...
 
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. More...
 

Data Fields

const YEAR = 'year'
 
const MONTH = 'month'
 
const WEEK = 'week'
 
const DAY = 'day'
 
const HOUR = 'hour'
 
const MINUTE = 'minute'
 

Protected Attributes

 $log
 
 $timezone = null
 
 $default_timezone = null
 
 $unix = 0
 

Detailed Description

@classDescription Date and time handling

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Version
$Id$

Definition at line 32 of file class.ilDateTime.php.

Constructor & Destructor Documentation

◆ __construct()

ilDateTime::__construct (   $a_date = null,
  $a_format = 0,
  $a_tz = '' 
)

Create new date object.

@access public

Parameters
mixedinteger string following the format given as the second parameter
intformat of date presentation

throws ilDateTimeException

Definition at line 60 of file class.ilDateTime.php.

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 }
const IL_CAL_UNIX
Class for DateTime exceptions.
setDate($a_date, $a_format)
set date
Class for TimeZone exceptions.
static _getInstance($a_tz='')
get instance by timezone

References $ilLog, ilTimeZone\_getInstance(), IL_CAL_UNIX, and setDate().

+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

ilDateTime::__toString ( )

to string for date time objects Output is user time zone

@access public

Parameters

return

Reimplemented in ilDate.

Definition at line 526 of file class.ilDateTime.php.

527 {
528 return $this->get(IL_CAL_DATETIME).'<br>';
529 }
const IL_CAL_DATETIME

References IL_CAL_DATETIME.

◆ _after()

static ilDateTime::_after ( ilDateTime  $start,
ilDateTime  $end,
  $a_compare_field = '',
  $a_tz = '' 
)
static

compare two dates and check start is after end This method does not consider tz offsets.

So you have to take care that both dates are defined in the the same timezone

@access public

Parameters
objectilDateTime
objectilDateTime
stringfield used for comparison. E.g IL_CAL_YEAR checks if start is one or more years after than end
stringtimezone

Definition at line 206 of file class.ilDateTime.php.

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 }
const IL_CAL_YEAR
const IL_CAL_MONTH
const IL_CAL_FKT_DATE
const IL_CAL_DAY
get($a_format, $a_format_str='', $a_tz='')
get formatted date

References get(), IL_CAL_DAY, IL_CAL_FKT_DATE, IL_CAL_MONTH, IL_CAL_UNIX, and IL_CAL_YEAR.

Referenced by ilObjGroupAccess\_registrationEnabled(), _within(), ilCalendarRecurrenceCalculator\applyLimits(), ilCalendarRecurrenceCalculator\calculateDateList(), ilCourseRegistrationGUI\fillRegistrationPeriod(), ilGroupRegistrationGUI\fillRegistrationPeriod(), ilConsultationHourBookingTableGUI\fillRow(), ilPageObject\getActive(), ilCalendarAppointmentPanelGUI\getHTML(), ilObjCourse\getSubItems(), ilBookingEntry\isAppointmentBookableForUser(), ilECSCategoryMappingRule\matchesValue(), ilObjGroup\register(), and ilECSCategoryMappingRule\validate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _before()

static ilDateTime::_before ( ilDateTime  $start,
ilDateTime  $end,
  $a_compare_field = '',
  $a_tz = '' 
)
static

compare two dates and check start is before end This method does not consider tz offsets.

So you have to take care that both dates are defined in the the same timezone

@access public

Parameters
objectilDateTime
objectilDateTime
stringfield used for comparison. E.g IL_CAL_YEAR checks if start is one or more years earlier than end
stringtimezone
Returns
bool

Definition at line 143 of file class.ilDateTime.php.

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 }

References get(), IL_CAL_DAY, IL_CAL_FKT_DATE, IL_CAL_MONTH, IL_CAL_UNIX, and IL_CAL_YEAR.

Referenced by ilObjGroupAccess\_registrationEnabled(), _within(), ilCalendarBlockGUI\addMiniMonth(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarRecurrenceCalculator\applyLimits(), ilCalendarRecurrenceCalculator\calculateDateList(), ilBookingEntry\cancelBooking(), ilCourseRegistrationGUI\fillRegistrationPeriod(), ilGroupRegistrationGUI\fillRegistrationPeriod(), ilECSServerTableGUI\fillRow(), ilPageObject\getActive(), ilCalendarBlockGUI\getHTML(), ilObjCourse\getSubItems(), ilECSTimePlace\loadFromJson(), ilObjCourseAccess\lookupRegistrationInfo(), ilObjGroupAccess\lookupRegistrationInfo(), ilECSCategoryMappingRule\matchesValue(), ilCalendarRecurrenceCalculator\optimizeStartingTime(), ilUserQuery\query(), ilObjGroup\register(), ilCalendarMonthGUI\show(), ilCalendarPresentationGUI\synchroniseExternalCalendars(), ilObjGroup\validate(), and ilCalendarEntry\validate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _equals()

static ilDateTime::_equals ( ilDateTime  $start,
ilDateTime  $end,
  $a_compare_field = '',
  $a_tz = '' 
)
static

Check if two date are equal.

@access public

Parameters
objectilDateTime
objectilDateTime
stringfield used for comparison. E.g IL_CAL_YEAR checks if start is the same years than end
stringtimzone

Definition at line 174 of file class.ilDateTime.php.

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 }

References get(), IL_CAL_DAY, IL_CAL_FKT_DATE, IL_CAL_MONTH, IL_CAL_UNIX, and IL_CAL_YEAR.

Referenced by ilCalendarUtil\_isToday(), _within(), ilCalendarBlockGUI\addMiniMonth(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarRecurrenceCalculator\calculateDateList(), ilCalendarAppointmentGUI\edit(), ilSubItemSelectionTableGUI\fillRow(), ilDatePresentation\formatPeriod(), ilDatePresentation\isToday(), ilDatePresentation\isTomorrow(), ilDatePresentation\isYesterday(), ilDateList\removeByDAY(), and ilCalendarMonthGUI\show().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _within()

static ilDateTime::_within ( ilDateTime  $dt,
ilDateTime  $start,
ilDateTime  $end,
  $a_compare_field = '',
  $a_tz = '' 
)
static

Check whether an date is within a date duration given by start and end.

Parameters
ilDateTime$dt
ilDateTime$start
ilDateTime$end
type$a_compare_field
type$a_tz

Definition at line 234 of file class.ilDateTime.php.

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 }
static _equals(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
Check if two date are equal.
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.
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.

References _after(), _before(), and _equals().

Referenced by ilObjCourse\getSubItems().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get()

ilDateTime::get (   $a_format,
  $a_format_str = '',
  $a_tz = '' 
)

get formatted date

@access public

Parameters
intformat type
stringformat string
stringa specific timezone

Definition at line 453 of file class.ilDateTime.php.

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 }
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
const IL_CAL_ISO_8601
const IL_CAL_TIMESTAMP
getUnixTime()
get unix time

References $default_timezone, $timezone, ilTimeZone\_getInstance(), getUnixTime(), IL_CAL_DATE, IL_CAL_DATETIME, IL_CAL_FKT_DATE, IL_CAL_FKT_GETDATE, IL_CAL_ISO_8601, IL_CAL_TIMESTAMP, and IL_CAL_UNIX.

Referenced by _after(), _before(), _equals(), ilDatePresentation\formatDate(), ilDatePresentation\formatPeriod(), ilCalendarRecurrenceCalculator\getYearWeekDays(), ilCalendarRegistration\isRegistered(), ilUtil\period2String(), and ilCalendarRegistration\unregister().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTimeZoneIdentifier()

ilDateTime::getTimeZoneIdentifier ( )

get timezone identifier

@access public

Definition at line 124 of file class.ilDateTime.php.

125 {
126 return $this->timezone->getIdentifier();
127 }

◆ getUnixTime()

ilDateTime::getUnixTime ( )

get unix time

@access public

Definition at line 295 of file class.ilDateTime.php.

296 {
297 return $this->unix;
298 }

References $unix.

Referenced by get().

+ Here is the caller graph for this function:

◆ getUTCOffset()

ilDateTime::getUTCOffset ( )

get UTC offset

@access public

Returns
offset to utc in seconds

Definition at line 307 of file class.ilDateTime.php.

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 }

◆ increment()

ilDateTime::increment (   $a_type,
  $a_count = 1 
)

increment

@access public

Parameters
inttype
intcount

Definition at line 249 of file class.ilDateTime.php.

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 }
@classDescription Date and time handling

References $d, $unix, DAY, HOUR, IL_CAL_UNIX, MINUTE, MONTH, WEEK, and YEAR.

Referenced by ilCalendarSchedule\initPeriod().

+ Here is the caller graph for this function:

◆ isNull()

ilDateTime::isNull ( )

Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)

Returns
bool

Definition at line 92 of file class.ilDateTime.php.

93 {
94 return $this->unix ? false : true;
95 }

Referenced by ilDatePresentation\formatDate().

+ Here is the caller graph for this function:

◆ setDate()

ilDateTime::setDate (   $a_date,
  $a_format 
)

set date

@access public

Parameters
mixeddate
intformat

Definition at line 324 of file class.ilDateTime.php.

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 }

References $timezone, $unix, ilTimeZone\_getInstance(), IL_CAL_DATE, IL_CAL_DATETIME, IL_CAL_FKT_GETDATE, IL_CAL_ISO_8601, IL_CAL_TIMESTAMP, and IL_CAL_UNIX.

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ switchTimeZone()

ilDateTime::switchTimeZone (   $a_timezone_identifier = '')

Switch timezone.

@access public

Parameters
stringPHP timezone identifier
Exceptions
ilDateTimeException

Definition at line 104 of file class.ilDateTime.php.

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 }

References ilTimeZone\_getInstance().

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $default_timezone

ilDateTime::$default_timezone = null
protected

Definition at line 44 of file class.ilDateTime.php.

Referenced by get().

◆ $log

ilDateTime::$log
protected

Definition at line 41 of file class.ilDateTime.php.

◆ $timezone

ilDateTime::$timezone = null
protected

Definition at line 43 of file class.ilDateTime.php.

Referenced by get(), and setDate().

◆ $unix

ilDateTime::$unix = 0
protected

Definition at line 46 of file class.ilDateTime.php.

Referenced by getUnixTime(), increment(), and setDate().

◆ DAY

◆ HOUR

const ilDateTime::HOUR = 'hour'

Definition at line 38 of file class.ilDateTime.php.

Referenced by ilConsultationHoursGUI\createAppointments(), and increment().

◆ MINUTE

const ilDateTime::MINUTE = 'minute'

Definition at line 39 of file class.ilDateTime.php.

Referenced by ilConsultationHoursGUI\createAppointments(), and increment().

◆ MONTH

◆ WEEK

◆ YEAR

const ilDateTime::YEAR = 'year'

The documentation for this class was generated from the following file: