5 include_once(
'Services/Calendar/classes/class.ilDateTimeException.php');
6 include_once(
'Services/Calendar/classes/class.ilTimeZone.php');
9 define(
'IL_CAL_DATETIME', 1);
10 define(
'IL_CAL_DATE', 2);
11 define(
'IL_CAL_UNIX', 3);
12 define(
'IL_CAL_FKT_DATE', 4);
13 define(
'IL_CAL_FKT_GETDATE', 5);
14 define(
'IL_CAL_TIMESTAMP', 6);
15 define(
'IL_CAL_ISO_8601', 7);
17 define(
'IL_CAL_YEAR',
'year');
18 define(
'IL_CAL_MONTH',
'month');
19 define(
'IL_CAL_WEEK',
'week');
20 define(
'IL_CAL_DAY',
'day');
21 define(
'IL_CAL_HOUR',
'hour');
22 define(
'IL_CAL_SECOND',
'second');
68 public function __construct($a_date = null, $a_format = 0, $a_tz =
'')
72 $this->log = $DIC->logger()->cal();
78 $this->
setDate($a_date, $a_format);
80 $this->log->warning($exc->getMessage());
94 return array(
'timezone',
'default_timezone',
'dt_obj');
101 $this->log = $DIC->logger()->cal();
111 return !($this->dt_obj instanceof
DateTime);
127 $this->log->warning(
'Unsupported timezone given: ' . $a_timezone_identifier);
128 throw new ilDateTimeException(
'Unsupported timezone given. Timezone: ' . $a_timezone_identifier);
140 return $this->timezone->getIdentifier();
163 switch ($a_compare_field) {
175 return $start->dt_obj < $end->dt_obj;
198 switch ($a_compare_field) {
210 return $start->dt_obj == $end->dt_obj;
233 switch ($a_compare_field) {
245 return $start->dt_obj > $end->dt_obj;
279 $sub = ($a_count < 0);
280 $count_str = abs($a_count);
284 $count_str .=
'year';
288 $count_str .=
'month';
292 $count_str .=
'week';
300 $count_str .=
'hour';
304 $count_str .=
'minute';
308 $count_str .=
'second';
312 $interval = date_interval_create_from_date_string($count_str);
314 $this->dt_obj->add($interval);
316 $this->dt_obj->sub($interval);
332 return $this->dt_obj->getTimestamp();
346 $offset = $this->dt_obj->getOffset();
354 protected function parsePartsToDate($a_year, $a_month, $a_day, $a_hour = null, $a_min = null, $a_sec = null, $a_timezone = null)
356 $a_year = (int) $a_year;
357 $a_month = (int) $a_month;
358 $a_day = (int) $a_day;
365 $a_hour = (int) $a_hour;
366 $a_min = (int) $a_min;
367 $a_sec = (int) $a_sec;
369 $format = $a_year .
'-' . $a_month .
'-' . $a_day;
371 if ($a_hour !== null) {
372 $format .=
' ' . (int) $a_hour .
':' . (
int) $a_min .
':' . (int) $a_sec;
403 $this->dt_obj = null;
412 $this->dt_obj =
new DateTime(
'@' . $a_date);
415 $message =
'Cannot parse date: ' . $a_date .
' with format ' . $a_format;
422 $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);
424 $this->log->warning(
'Cannot parse date: ' . $a_date);
425 $this->log->warning(print_r($matches,
true));
430 $tz_id = ($d_parts[9] ==
'Z')
449 $this->log->warning(
'Cannot parse date : ' . $a_date);
468 if (preg_match(
"/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date, $d_parts) ==
false) {
469 $this->log->warning(
'Cannot parse date: ' . $a_date);
484 $this->dt_obj = DateTime::createFromFormat(
506 public function get($a_format, $a_format_str =
'', $a_tz =
'')
516 $this->log->warning(
'Invalid timezone given. Timezone: ' . $a_tz);
522 $out_date = clone($this->dt_obj);
532 $date = $out_date->format(
'Y-m-d');
536 $date = $out_date->format(
'Y-m-d H:i:s');
540 $date = $out_date->format($a_format_str);
545 'seconds' => (
int) $out_date->format(
's')
546 ,
'minutes' => (int) $out_date->format(
'i')
547 ,
'hours' => (int) $out_date->format(
'G')
548 ,
'mday' => (int) $out_date->format(
'j')
549 ,
'wday' => (int) $out_date->format(
'w')
550 ,
'mon' => (int) $out_date->format(
'n')
551 ,
'year' => (int) $out_date->format(
'Y')
552 ,
'yday' => (int) $out_date->format(
'z')
553 ,
'weekday' => $out_date->format(
'l')
554 ,
'month' => $out_date->format(
'F')
555 ,
'isoday' => (int) $out_date->format(
'N')
560 $date = $out_date->format(
'c');
564 $date = $out_date->format(
'YmdHis');
__construct($a_date=null, $a_format=0, $a_tz='')
Create new date object.
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.
switchTimeZone($a_timezone_identifier='')
Switch timezone.
Class for DateTime exceptions.
__toString()
to string for date time objects Output is user time zone
getTimeZoneIdentifier()
get timezone identifier
static _equals(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
Check if two date are equal.
catch(Exception $e) $message
getUTCOffset()
get UTC offset
Class for TimeZone exceptions.
get($a_format, $a_format_str='', $a_tz='')
get formatted date
increment($a_type, $a_count=1)
increment
static _getInstance($a_tz='')
get instance by timezone
getUnixTime()
get unix time
setDate($a_date, $a_format)
Set date.
parsePartsToDate($a_year, $a_month, $a_day, $a_hour=null, $a_min=null, $a_sec=null, $a_timezone=null)
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.
isNull()
Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)