ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilDateTime Class Reference

Date and time handling More...

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

Public Member Functions

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

Static Public Member Functions

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

Protected Member Functions

 parsePartsToDate (int $a_year, int $a_month, int $a_day, ?int $a_hour=null, ?int $a_min=null, ?int $a_sec=null, ?string $a_timezone=null)
 

Protected Attributes

ilLogger $log
 
ilTimeZone $timezone = null
 
ilTimeZone $default_timezone = null
 
DateTime $dt_obj = null
 

Detailed Description

Date and time handling

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

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

Constructor & Destructor Documentation

◆ __construct()

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

Create new date object.

Parameters
int|stringfollowing the format given as the second parameter
intformat
string
Exceptions
ilDateTimeException

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

References $DIC, ilTimeZone\_getInstance(), and setDate().

51  {
52  global $DIC;
53 
54  $this->log = $DIC->logger()->cal();
55 
56  try {
57  $this->timezone = ilTimeZone::_getInstance($a_tz);
58  $this->default_timezone = ilTimeZone::_getInstance('');
59  $this->setDate($a_date, $a_format);
60  } catch (ilTimeZoneException $exc) {
61  $this->log->warning($exc->getMessage());
62  throw new ilDateTimeException('Unsupported timezone given. Timezone: ' . $a_tz);
63  }
64  }
static _getInstance(string $a_tz='')
get instance by timezone
setDate($a_date, int $a_format)
Set date.
global $DIC
Definition: feed.php:28
Class for TimeZone exceptions.
+ Here is the call graph for this function:

Member Function Documentation

◆ __clone()

ilDateTime::__clone ( )

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

References $dt_obj.

67  {
68  if ($this->dt_obj) {
69  $this->dt_obj = clone $this->dt_obj;
70  }
71  }
DateTime $dt_obj

◆ __sleep()

ilDateTime::__sleep ( )

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

74  {
75  return array('timezone', 'default_timezone', 'dt_obj');
76  }

◆ __toString()

ilDateTime::__toString ( )

to string for date time objects Output is user time zone public

Parameters

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

References IL_CAL_DATETIME.

546  : string
547  {
548  return $this->get(IL_CAL_DATETIME) . '<br>';
549  }
const IL_CAL_DATETIME

◆ __wakeup()

ilDateTime::__wakeup ( )

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

References $DIC.

79  {
80  global $DIC;
81  $this->log = $DIC->logger()->cal();
82  }
global $DIC
Definition: feed.php:28

◆ _after()

static ilDateTime::_after ( ilDateTime  $start,
ilDateTime  $end,
string  $a_compare_field = '',
string  $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 public

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

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

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

Referenced by ilObjGroupAccess\_registrationEnabled(), _within(), ilCalendarRecurrenceCalculator\applyDurationPeriod(), ilCalendarRecurrenceCalculator\applyLimits(), ilCalendarRecurrenceCalculator\calculateDateList(), ilDateDurationInputGUI\checkInput(), ilAppointmentPresentationConsultationHoursGUI\collectPropertiesAndActions(), ilCourseRegistrationGUI\fillRegistrationPeriod(), ilGroupRegistrationGUI\fillRegistrationPeriod(), ilConsultationHourBookingTableGUI\fillRow(), ilSearchResult\filter(), ilSessionClassificationPathGUI\findSessionContainerForItem(), ilPageObject\getActive(), ilCalendarAppointmentPanelGUI\getHTML(), ilCalendarAgendaListGUI\getHTML(), ilContainerSessionsContentGUI\initDetails(), ilBookingEntry\isAppointmentBookableForUser(), ilECSCategoryMappingRule\matchesValue(), ilObjGroup\register(), ilObjectActivationGUI\update(), ilCourseContentGUI\updateManagedTimings(), ilCourseContentGUI\updatePersonalTimings(), and ilECSCategoryMappingRule\validate().

202  : bool {
203  if ($start->isNull() || $end->isNull()) {
204  return false;
205  }
206 
207  switch ($a_compare_field) {
208  case IL_CAL_YEAR:
209  return $start->get(IL_CAL_FKT_DATE, 'Y', $a_tz) > $end->get(IL_CAL_FKT_DATE, 'Y', $a_tz);
210 
211  case IL_CAL_MONTH:
212  return (int) $start->get(IL_CAL_FKT_DATE, 'Ym', $a_tz) > $end->get(IL_CAL_FKT_DATE, 'Ym', $a_tz);
213 
214  case IL_CAL_DAY:
215  return (int) $start->get(IL_CAL_FKT_DATE, 'Ymd', $a_tz) > $end->get(IL_CAL_FKT_DATE, 'Ymd', $a_tz);
216 
217  case '':
218  default:
219  return $start->dt_obj > $end->dt_obj;
220 
221  }
222  }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
const IL_CAL_MONTH
const IL_CAL_DAY
const IL_CAL_FKT_DATE
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)
const IL_CAL_YEAR
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _before()

static ilDateTime::_before ( ilDateTime  $start,
ilDateTime  $end,
string  $a_compare_field = '',
string  $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

Parameters
ilDateTime
ilDateTime
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 124 of file class.ilDateTime.php.

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

Referenced by ilObjGroupAccess\_registrationEnabled(), _within(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarBlockGUI\addMiniMonth(), ilCalendarRecurrenceCalculator\applyDurationPeriod(), ilCalendarRecurrenceCalculator\applyLimits(), ilCalendarRecurrenceCalculator\calculateDateList(), ilBookingEntry\cancelBooking(), ilCourseRegistrationGUI\fillRegistrationPeriod(), ilGroupRegistrationGUI\fillRegistrationPeriod(), ilECSServerTableGUI\fillRow(), ilSearchResult\filter(), ilPageObject\getActive(), ilConsultationHourUtils\getConsultationHourLinksForRepositoryObject(), ilCalendarAgendaListGUI\getHTML(), ilCalendarBlockGUI\getHTML(), ilExerciseManagementGUI\handleIndividualDeadlineCallsObject(), ilContainerSessionsContentGUI\initDetails(), ilECSTimePlace\loadFromJson(), ilObjGroupAccess\lookupRegistrationInfo(), ilObjCourseAccess\lookupRegistrationInfo(), ilECSCategoryMappingRule\matchesValue(), ilCalendarRecurrenceCalculator\optimizeEndingTime(), ilCalendarRecurrenceCalculator\optimizeStartingTime(), ilUserQuery\query(), ilObjGroup\register(), ilCalendarMonthGUI\show(), and ilCalendarEntry\validate().

129  : bool {
130  if ($start->isNull() || $end->isNull()) {
131  return false;
132  }
133 
134  switch ($a_compare_field) {
135  case IL_CAL_YEAR:
136  return $start->get(IL_CAL_FKT_DATE, 'Y', $a_tz) < $end->get(IL_CAL_FKT_DATE, 'Y', $a_tz);
137 
138  case IL_CAL_MONTH:
139  return (int) $start->get(IL_CAL_FKT_DATE, 'Ym', $a_tz) < $end->get(IL_CAL_FKT_DATE, 'Ym', $a_tz);
140 
141  case IL_CAL_DAY:
142  return (int) $start->get(IL_CAL_FKT_DATE, 'Ymd', $a_tz) < $end->get(IL_CAL_FKT_DATE, 'Ymd', $a_tz);
143 
144  case '':
145  default:
146  return $start->dt_obj < $end->dt_obj;
147 
148  }
149  }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
const IL_CAL_MONTH
const IL_CAL_DAY
const IL_CAL_FKT_DATE
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)
const IL_CAL_YEAR
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _equals()

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

Check if two date are equal.

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

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

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

Referenced by ilCalendarUtil\_isToday(), _within(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarBlockGUI\addMiniMonth(), ilCalendarRecurrenceCalculator\calculateDateList(), ilCalendarAppointmentGUI\edit(), ilSubItemSelectionTableGUI\fillRow(), ilSearchResult\filter(), ilSessionClassificationPathGUI\findSessionContainerForItem(), ilDatePresentation\formatPeriod(), ilCalendarAgendaListGUI\getHTML(), ilDatePresentation\isToday(), ilDatePresentation\isTomorrow(), ilDatePresentation\isYesterday(), ilDateList\removeByDAY(), ilEmployeeTalkTableGUI\setTalkData(), and ilCalendarMonthGUI\show().

164  : bool {
165  if ($start->isNull() || $end->isNull()) {
166  return false;
167  }
168 
169  switch ($a_compare_field) {
170  case IL_CAL_YEAR:
171  return $start->get(IL_CAL_FKT_DATE, 'Y', $a_tz) == $end->get(IL_CAL_FKT_DATE, 'Y', $a_tz);
172 
173  case IL_CAL_MONTH:
174  return (int) $start->get(IL_CAL_FKT_DATE, 'Ym', $a_tz) == $end->get(IL_CAL_FKT_DATE, 'Ym', $a_tz);
175 
176  case IL_CAL_DAY:
177  return (int) $start->get(IL_CAL_FKT_DATE, 'Ymd', $a_tz) == $end->get(IL_CAL_FKT_DATE, 'Ymd', $a_tz);
178 
179  case '':
180  default:
181  return $start->dt_obj == $end->dt_obj;
182 
183  }
184  }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
const IL_CAL_MONTH
const IL_CAL_DAY
const IL_CAL_FKT_DATE
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)
const IL_CAL_YEAR
+ 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,
string  $a_compare_field = '',
string  $a_tz = '' 
)
static

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

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

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

Referenced by ilCalendarScheduleFilterExercise\addCustomEvents(), ilCalendarScheduleFilterTimings\addCustomEvents(), ilCalendarHeaderNavigationGUI\getHTML(), and ilContainerSessionsContentGUI\initDetails().

233  : bool {
234  return
235  (ilDateTime::_after($dt, $start, $a_compare_field, $a_tz) or ilDateTime::_equals(
236  $dt,
237  $start,
238  $a_compare_field,
239  $a_tz
240  )) &&
241  (ilDateTime::_before($dt, $end, $a_compare_field, $a_tz) or ilDateTime::_equals(
242  $dt,
243  $end,
244  $a_compare_field,
245  $a_tz
246  ));
247  }
static _before(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
static _after(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
static _equals(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
Check if two date are equal.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get()

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

get formatted date

Returns
string|int|array|null

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

References $default_timezone, ilTimeZone\_getInstance(), ilTimeZone\getIdentifier(), getUnixTime(), IL_CAL_DATE, IL_CAL_DATETIME, IL_CAL_FKT_DATE, IL_CAL_FKT_GETDATE, IL_CAL_ISO_8601, IL_CAL_TIMESTAMP, IL_CAL_UNIX, ILIAS\Repository\int(), and isNull().

Referenced by _after(), _before(), _equals(), ilDateList\add(), ilCalendarWeekGUI\addAppointmentLink(), ilConsultationHourUtils\findCalendarAppointmentsForBooking(), ilDatePresentation\formatDate(), ilDatePresentation\formatPeriod(), ilCmiXapiDateTime\fromIliasDateTime(), ilCalendarSchedule\getByDay(), ilCalendarHeaderNavigationGUI\getHTML(), ilCalendarRegistration\getRegisteredUsers(), ilCalendarRecurrenceCalculator\getYearWeekDays(), ilCalendarRegistration\isRegistered(), ilLTIDataConnector\lookupResourcesForAllUsersSinceDate(), ilCalendarWeekGUI\parseHourInfo(), ilLegacyFormElementsUtil\period2String(), ilExcel\prepareDateValue(), ilChatroomHistoryGUI\renderDateTimeInformation(), ilObjEmployeeTalkGUI\sendNotification(), ilEmployeeTalkAppointmentGUI\sendNotification(), ilObjEmployeeTalkSeriesGUI\sendNotification(), ilObjEmployeeTalkGUI\sendUpdateNotification(), ilExAssignment\setIndividualDeadline(), ilCalendarRegistration\unregister(), and ilCmiXapiUser\updateFetchedUntilForObjects().

467  {
468  if ($this->isNull()) {
469  return null;
470  }
471 
473  if ($a_tz) {
474  try {
476  } catch (ilTimeZoneException $exc) {
477  $this->log->warning('Invalid timezone given. Timezone: ' . $a_tz);
478  }
479  }
480  $out_date = clone($this->dt_obj);
481  $out_date->setTimezone(new DateTimeZone($timezone->getIdentifier()));
482 
483  $date = null;
484  switch ($a_format) {
485  case IL_CAL_UNIX:
486  // timezone unrelated
487  $date = $this->getUnixTime();
488  break;
489 
490  case IL_CAL_DATE:
491  $date = $out_date->format('Y-m-d');
492  break;
493 
494  case IL_CAL_DATETIME:
495  $date = $out_date->format('Y-m-d H:i:s');
496  break;
497 
498  case IL_CAL_FKT_DATE:
499  $date = $out_date->format($a_format_str);
500  break;
501 
502  case IL_CAL_FKT_GETDATE:
503  $date = array(
504  'seconds' => (int) $out_date->format('s')
505  ,
506  'minutes' => (int) $out_date->format('i')
507  ,
508  'hours' => (int) $out_date->format('G')
509  ,
510  'mday' => (int) $out_date->format('j')
511  ,
512  'wday' => (int) $out_date->format('w')
513  ,
514  'mon' => (int) $out_date->format('n')
515  ,
516  'year' => (int) $out_date->format('Y')
517  ,
518  'yday' => (int) $out_date->format('z')
519  ,
520  'weekday' => $out_date->format('l')
521  ,
522  'month' => $out_date->format('F')
523  ,
524  'isoday' => (int) $out_date->format('N')
525  );
526  break;
527 
528  case IL_CAL_ISO_8601:
529  $date = $out_date->format('c');
530  break;
531 
532  case IL_CAL_TIMESTAMP:
533  $date = $out_date->format('YmdHis');
534  break;
535  }
536  return $date;
537  }
const IL_CAL_DATETIME
static _getInstance(string $a_tz='')
get instance by timezone
ilTimeZone $timezone
ilTimeZone $default_timezone
const IL_CAL_UNIX
Class for TimeZone exceptions.
const IL_CAL_FKT_DATE
const IL_CAL_ISO_8601
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
const IL_CAL_TIMESTAMP
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTimeZoneIdentifier()

ilDateTime::getTimeZoneIdentifier ( )

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

Referenced by parsePartsToDate(), and setDate().

109  : string
110  {
111  return $this->timezone->getIdentifier();
112  }
+ Here is the caller graph for this function:

◆ getUnixTime()

ilDateTime::getUnixTime ( )

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

References isNull().

Referenced by get(), ilChatroom\getPrivateRoomSessions(), increment(), ilObjEmployeeTalkGUI\sendNotification(), ilEmployeeTalkAppointmentGUI\sendNotification(), ilObjEmployeeTalkSeriesGUI\sendNotification(), and ilObjEmployeeTalkGUI\sendUpdateNotification().

303  : ?int
304  {
305  if (!$this->isNull()) {
306  return $this->dt_obj->getTimestamp();
307  }
308  return null;
309  }
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ increment()

ilDateTime::increment ( string  $a_type,
int  $a_count = 1 
)
Parameters
string$a_type
int$a_count
Returns
int|null
Todo:
refactor return type

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

References getUnixTime(), and isNull().

Referenced by ilCalendarRecurrenceCalculator\applyBYDAYRules(), ilCalendarSchedule\getByDay(), ilCalendarSchedule\getChangedEvents(), ilTimingsUser\handleNewMembership(), ilCalendarRecurrenceCalculator\incrementByFrequency(), ilCalendarAppointmentGUI\initAppointment(), ilCalendarAgendaListGUI\initEndPeriod(), and ilCalendarSchedule\initPeriod().

255  : ?int
256  {
257  if ($this->isNull()) {
258  return null;
259  }
260 
261  $sub = ($a_count < 0);
262  $count_str = abs($a_count);
263 
264  switch ($a_type) {
265  case self::YEAR:
266  $count_str .= 'year';
267  break;
268 
269  case self::MONTH:
270  $count_str .= 'month';
271  break;
272 
273  case self::WEEK:
274  $count_str .= 'week';
275  break;
276 
277  case self::DAY:
278  $count_str .= 'day';
279  break;
280 
281  case self::HOUR:
282  $count_str .= 'hour';
283  break;
284 
285  case self::MINUTE:
286  $count_str .= 'minute';
287  break;
288 
289  case self::SECOND:
290  $count_str .= 'second';
291  break;
292  }
293 
294  $interval = date_interval_create_from_date_string($count_str);
295  if (!$sub) {
296  $this->dt_obj->add($interval);
297  } else {
298  $this->dt_obj->sub($interval);
299  }
300  return $this->getUnixTime();
301  }
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)
+ Here is the call graph for this function:
+ 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 88 of file class.ilDateTime.php.

Referenced by _after(), _before(), _equals(), ilDatePresentation\formatDate(), get(), getUnixTime(), and increment().

88  : bool
89  {
90  return !($this->dt_obj instanceof DateTime);
91  }
+ Here is the caller graph for this function:

◆ parsePartsToDate()

ilDateTime::parsePartsToDate ( int  $a_year,
int  $a_month,
int  $a_day,
?int  $a_hour = null,
?int  $a_min = null,
?int  $a_sec = null,
?string  $a_timezone = null 
)
protected

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

References $format, getTimeZoneIdentifier(), and ILIAS\Repository\int().

Referenced by setDate().

319  : ?DateTime {
320  $a_year = $a_year;
321  $a_month = $a_month;
322  $a_day = $a_day;
323 
324  if (!$a_year) {
325  return null;
326  }
327  $date = null;
328  try {
329  $a_hour = (int) $a_hour;
330  $a_min = (int) $a_min;
331  $a_sec = (int) $a_sec;
332 
333  $format = $a_year . '-' . $a_month . '-' . $a_day;
334 
335  if ($a_hour !== null) {
336  $format .= ' ' . $a_hour . ':' . $a_min . ':' . $a_sec;
337 
338  // use current timezone if no other given
339  if (!$a_timezone) {
340  $a_timezone = $this->getTimeZoneIdentifier();
341  }
342 
343  $date = new DateTime($format, new DateTimeZone($a_timezone));
344  } else {
345  $date = new DateTime($format);
346  }
347  } catch (Exception $ex) {
348  // :TODO: do anything?
349  }
350  return ($date instanceof DateTime)
351  ? $date
352  : null;
353  }
$format
Definition: metadata.php:235
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setDate()

ilDateTime::setDate (   $a_date,
int  $a_format 
)

Set date.

Exceptions
ilDateTimeException
Todo:
fix ISO_8601 support

False "DateTime::ATOM was removed 7.2 warning" is a false positve

PhpElementIsNotAvailableInCurrentPhpVersionInspection

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

References $message, getTimeZoneIdentifier(), IL_CAL_DATE, IL_CAL_DATETIME, IL_CAL_FKT_GETDATE, IL_CAL_ISO_8601, IL_CAL_TIMESTAMP, IL_CAL_UNIX, parsePartsToDate(), and ilLogLevel\WARNING.

Referenced by __construct(), and ilExAssignmentEditorGUI\setDisabledFieldValues().

360  : void
361  {
362  $this->dt_obj = null;
363 
364  if (!$a_date) {
365  return;
366  }
367 
368  switch ($a_format) {
369  case IL_CAL_UNIX:
370  try {
371  $this->dt_obj = new DateTime('@' . $a_date);
372  $this->dt_obj->setTimezone(new DateTimeZone($this->getTimeZoneIdentifier()));
373  } catch (Exception $ex) {
374  $message = 'Cannot parse date: ' . $a_date . ' with format ' . $a_format;
375  $this->log->warning($message);
376  throw new ilDateTimeException($message);
377  }
378  break;
379 
380  case IL_CAL_DATETIME:
381  $matches = preg_match(
382  '/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)$/i',
383  $a_date,
384  $d_parts
385  );
386  if ($matches < 1) {
387  $this->log->warning('Cannot parse date: ' . $a_date);
388  $this->log->warning(print_r($matches, true));
389  $this->log->logStack(ilLogLevel::WARNING);
390  throw new ilDateTimeException('Cannot parse date: ' . $a_date);
391  }
392 
393  $tz_id = (isset($d_parts[9]) && $d_parts[9] === 'Z')
394  ? 'UTC'
395  : $this->getTimeZoneIdentifier();
396  $this->dt_obj = $this->parsePartsToDate(
397  (int) $d_parts[1],
398  (int) $d_parts[2],
399  (int) $d_parts[3],
400  (int) $d_parts[5],
401  (int) $d_parts[6],
402  (int) $d_parts[7],
403  $tz_id
404  );
405  break;
406 
407  case IL_CAL_DATE:
408  try {
409  // Pure dates are not timezone sensible.
410  $this->dt_obj = new DateTime($a_date, new DateTimeZone('UTC'));
411  } catch (Exception $ex) {
412  $this->log->warning('Cannot parse date : ' . $a_date);
413  throw new ilDateTimeException('Cannot parse date: ' . $a_date);
414  }
415  break;
416 
417  case IL_CAL_FKT_GETDATE:
418  // Format like getdate parameters
419  $this->dt_obj = $this->parsePartsToDate(
420  (int) $a_date['year'],
421  (int) $a_date['mon'],
422  (int) $a_date['mday'],
423  (int) $a_date['hours'],
424  (int) $a_date['minutes'],
425  (int) ($a_date['seconds'] ?? 0),
426  $this->getTimeZoneIdentifier()
427  );
428  break;
429 
430  case IL_CAL_TIMESTAMP:
431  if (!preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date, $d_parts)) {
432  $this->log->warning('Cannot parse date: ' . $a_date);
433  throw new ilDateTimeException('Cannot parse date.');
434  }
435  $this->dt_obj = $this->parsePartsToDate(
436  (int) $d_parts[1],
437  (int) $d_parts[2],
438  (int) $d_parts[3],
439  (int) $d_parts[4],
440  (int) $d_parts[5],
441  (int) $d_parts[6],
442  $this->getTimeZoneIdentifier()
443  );
444  break;
445 
446  case IL_CAL_ISO_8601:
451  $this->dt_obj = DateTime::createFromFormat(
452  DateTime::ATOM,
453  $a_date,
454  new DateTimeZone($this->getTimeZoneIdentifier())
455  );
456  break;
457  }
458  // remove set timezone since it does not influence the internal date.
459  // the tz must be passed in the moment of the creation of the date object.
460  }
const IL_CAL_DATETIME
parsePartsToDate(int $a_year, int $a_month, int $a_day, ?int $a_hour=null, ?int $a_min=null, ?int $a_sec=null, ?string $a_timezone=null)
const IL_CAL_UNIX
const IL_CAL_ISO_8601
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
$message
Definition: xapiexit.php:32
const IL_CAL_TIMESTAMP
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ switchTimeZone()

ilDateTime::switchTimeZone ( string  $a_timezone_identifier = '')

Switch timezone.

Parameters
stringPHP timezone identifier
Exceptions
ilDateTimeException

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

References Vendor\Package\$e, and ilTimeZone\_getInstance().

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

98  : void
99  {
100  try {
101  $this->timezone = ilTimeZone::_getInstance($a_timezone_identifier);
102  return;
103  } catch (ilTimeZoneException $e) {
104  $this->log->warning('Unsupported timezone given: ' . $a_timezone_identifier);
105  throw new ilDateTimeException('Unsupported timezone given. Timezone: ' . $a_timezone_identifier);
106  }
107  }
static _getInstance(string $a_tz='')
get instance by timezone
Class for TimeZone exceptions.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $default_timezone

ilTimeZone ilDateTime::$default_timezone = null
protected

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

Referenced by get().

◆ $dt_obj

DateTime ilDateTime::$dt_obj = null
protected

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

Referenced by __clone().

◆ $log

ilLogger ilDateTime::$log
protected

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

◆ $timezone

ilTimeZone ilDateTime::$timezone = null
protected

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

◆ DAY

◆ HOUR

const ilDateTime::HOUR = 'hour'

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

Referenced by ilConsultationHoursGUI\createAppointments().

◆ MINUTE

const ilDateTime::MINUTE = 'minute'

◆ MONTH

◆ SECOND

const ilDateTime::SECOND = 'second'

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

◆ WEEK

◆ YEAR


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