3 declare(strict_types=1);
30 public const YEAR =
'year';
32 public const WEEK =
'week';
33 public const DAY =
'day';
34 public const HOUR =
'hour';
50 public function __construct($a_date = null,
int $a_format = 0,
string $a_tz =
'')
54 $this->log = $DIC->logger()->cal();
59 $this->
setDate($a_date, $a_format);
61 $this->log->warning($exc->getMessage());
75 return array(
'timezone',
'default_timezone',
'dt_obj');
81 $this->log = $DIC->logger()->cal();
90 return !($this->dt_obj instanceof
DateTime);
104 $this->log->warning(
'Unsupported timezone given: ' . $a_timezone_identifier);
105 throw new ilDateTimeException(
'Unsupported timezone given. Timezone: ' . $a_timezone_identifier);
111 return $this->timezone->getIdentifier();
127 string $a_compare_field =
'',
134 switch ($a_compare_field) {
146 return $start->dt_obj < $end->dt_obj;
161 string $a_compare_field =
'',
168 switch ($a_compare_field) {
180 return $start->dt_obj == $end->dt_obj;
198 string $a_compare_field =
'',
205 switch ($a_compare_field) {
217 return $start->dt_obj > $end->dt_obj;
228 string $a_compare_field =
'',
258 $sub = ($a_count < 0);
259 $count_str = abs($a_count);
263 $count_str .=
'year';
267 $count_str .=
'month';
271 $count_str .=
'week';
279 $count_str .=
'hour';
283 $count_str .=
'minute';
287 $count_str .=
'second';
291 $interval = date_interval_create_from_date_string($count_str);
293 $this->dt_obj->add($interval);
295 $this->dt_obj->sub($interval);
303 return $this->dt_obj->getTimestamp();
315 ?
string $a_timezone = null
326 $a_hour = (
int) $a_hour;
327 $a_min = (
int) $a_min;
328 $a_sec = (
int) $a_sec;
330 $format = $a_year .
'-' . $a_month .
'-' . $a_day;
332 if ($a_hour !== null) {
333 $format .=
' ' . $a_hour .
':' . $a_min .
':' . $a_sec;
357 public function setDate($a_date,
int $a_format): void
359 $this->dt_obj = null;
368 $this->dt_obj =
new DateTime(
'@' . $a_date);
371 $message =
'Cannot parse date: ' . $a_date .
' with format ' . $a_format;
378 $matches = preg_match(
379 '/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)$/i',
384 $this->log->warning(
'Cannot parse date: ' . $a_date);
385 $this->log->warning(print_r($matches,
true));
390 $tz_id = (isset($d_parts[9]) && $d_parts[9] ===
'Z')
409 $this->log->warning(
'Cannot parse date : ' . $a_date);
417 (
int) $a_date[
'year'],
418 (
int) $a_date[
'mon'],
419 (
int) $a_date[
'mday'],
420 (
int) $a_date[
'hours'],
421 (
int) $a_date[
'minutes'],
422 (
int) ($a_date[
'seconds'] ?? 0),
428 if (!preg_match(
"/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date, $d_parts)) {
429 $this->log->warning(
'Cannot parse date: ' . $a_date);
448 $this->dt_obj = DateTime::createFromFormat(
463 public function get(
int $a_format,
string $a_format_str =
'',
string $a_tz =
'')
474 $this->log->warning(
'Invalid timezone given. Timezone: ' . $a_tz);
477 $out_date = clone($this->dt_obj);
488 $date = $out_date->format(
'Y-m-d');
492 $date = $out_date->format(
'Y-m-d H:i:s');
496 $date = $out_date->format($a_format_str);
501 'seconds' => (
int) $out_date->format(
's')
503 'minutes' => (
int) $out_date->format(
'i')
505 'hours' => (
int) $out_date->format(
'G')
507 'mday' => (
int) $out_date->format(
'j')
509 'wday' => (
int) $out_date->format(
'w')
511 'mon' => (
int) $out_date->format(
'n')
513 'year' => (
int) $out_date->format(
'Y')
515 'yday' => (
int) $out_date->format(
'z')
517 'weekday' => $out_date->format(
'l')
519 'month' => $out_date->format(
'F')
521 'isoday' => (
int) $out_date->format(
'N')
526 $date = $out_date->format(
'c');
530 $date = $out_date->format(
'YmdHis');
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
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)
static _getInstance(string $a_tz='')
get instance by timezone
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.
ilTimeZone $default_timezone
setDate($a_date, int $a_format)
Set date.
increment(string $a_type, int $a_count=1)
__toString()
to string for date time objects Output is user time zone public
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.
Class for TimeZone exceptions.
static _equals(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
Check if two date are equal.
__construct($a_date=null, int $a_format=0, string $a_tz='')
Create new date object.
switchTimeZone(string $a_timezone_identifier='')
Switch timezone.
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.
isNull()
Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)