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;
162 string $a_compare_field =
'',
169 switch ($a_compare_field) {
181 return $start->dt_obj == $end->dt_obj;
200 string $a_compare_field =
'',
207 switch ($a_compare_field) {
219 return $start->dt_obj > $end->dt_obj;
231 string $a_compare_field =
'',
261 $sub = ($a_count < 0);
262 $count_str = abs($a_count);
266 $count_str .=
'year';
270 $count_str .=
'month';
274 $count_str .=
'week';
282 $count_str .=
'hour';
286 $count_str .=
'minute';
290 $count_str .=
'second';
294 $interval = date_interval_create_from_date_string($count_str);
296 $this->dt_obj->add($interval);
298 $this->dt_obj->sub($interval);
306 return $this->dt_obj->getTimestamp();
318 ?
string $a_timezone = null
329 $a_hour = (
int) $a_hour;
330 $a_min = (
int) $a_min;
331 $a_sec = (
int) $a_sec;
333 $format = $a_year .
'-' . $a_month .
'-' . $a_day;
335 if ($a_hour !== null) {
336 $format .=
' ' . $a_hour .
':' . $a_min .
':' . $a_sec;
360 public function setDate($a_date,
int $a_format): void
362 $this->dt_obj = null;
371 $this->dt_obj =
new DateTime(
'@' . $a_date);
374 $message =
'Cannot parse date: ' . $a_date .
' with format ' . $a_format;
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',
387 $this->log->warning(
'Cannot parse date: ' . $a_date);
388 $this->log->warning(print_r($matches,
true));
393 $tz_id = (isset($d_parts[9]) && $d_parts[9] ===
'Z')
412 $this->log->warning(
'Cannot parse date : ' . $a_date);
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),
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);
451 $this->dt_obj = DateTime::createFromFormat(
466 public function get(
int $a_format,
string $a_format_str =
'',
string $a_tz =
'')
477 $this->log->warning(
'Invalid timezone given. Timezone: ' . $a_tz);
480 $out_date = clone($this->dt_obj);
491 $date = $out_date->format(
'Y-m-d');
495 $date = $out_date->format(
'Y-m-d H:i:s');
499 $date = $out_date->format($a_format_str);
504 'seconds' => (
int) $out_date->format(
's')
506 'minutes' => (
int) $out_date->format(
'i')
508 'hours' => (
int) $out_date->format(
'G')
510 'mday' => (
int) $out_date->format(
'j')
512 'wday' => (
int) $out_date->format(
'w')
514 'mon' => (
int) $out_date->format(
'n')
516 'year' => (
int) $out_date->format(
'Y')
518 'yday' => (
int) $out_date->format(
'z')
520 'weekday' => $out_date->format(
'l')
522 'month' => $out_date->format(
'F')
524 'isoday' => (
int) $out_date->format(
'N')
529 $date = $out_date->format(
'c');
533 $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,...)