19declare(strict_types=1);
44 public const YEAR =
'year';
46 public const WEEK =
'week';
47 public const DAY =
'day';
48 public const HOUR =
'hour';
64 public function __construct($a_date =
null,
int $a_format = 0,
string $a_tz =
'')
68 $this->log =
$DIC->logger()->cal();
73 $this->
setDate($a_date, $a_format);
75 $this->log->warning($exc->getMessage());
89 return array(
'timezone',
'default_timezone',
'dt_obj');
95 $this->log =
$DIC->logger()->cal();
104 return !($this->dt_obj instanceof DateTime);
118 $this->log->warning(
'Unsupported timezone given: ' . $a_timezone_identifier);
119 throw new ilDateTimeException(
'Unsupported timezone given. Timezone: ' . $a_timezone_identifier);
125 return $this->timezone->getIdentifier();
141 string $a_compare_field =
'',
148 switch ($a_compare_field) {
160 return $start->dt_obj < $end->dt_obj;
175 string $a_compare_field =
'',
178 if ($start->isNull() || $end->isNull()) {
182 switch ($a_compare_field) {
194 return $start->dt_obj == $end->dt_obj;
212 string $a_compare_field =
'',
215 if ($start->isNull() || $end->isNull()) {
219 switch ($a_compare_field) {
231 return $start->dt_obj > $end->dt_obj;
242 string $a_compare_field =
'',
268 if ($this->isNull()) {
272 $sub = ($a_count < 0);
273 $count_str = abs($a_count);
277 $count_str .=
'year';
281 $count_str .=
'month';
285 $count_str .=
'week';
293 $count_str .=
'hour';
297 $count_str .=
'minute';
301 $count_str .=
'second';
305 $interval = date_interval_create_from_date_string($count_str);
307 $this->dt_obj->add($interval);
309 $this->dt_obj->sub($interval);
311 return $this->getUnixTime();
316 if (!$this->isNull()) {
317 return $this->dt_obj->getTimestamp();
329 ?
string $a_timezone =
null
340 $a_hour = (
int) $a_hour;
341 $a_min = (
int) $a_min;
342 $a_sec = (
int) $a_sec;
344 $format = $a_year .
'-' . $a_month .
'-' . $a_day;
346 if ($a_hour !==
null) {
347 $format .=
' ' . $a_hour .
':' . $a_min .
':' . $a_sec;
351 $a_timezone = $this->getTimeZoneIdentifier();
354 $date =
new DateTime($format,
new DateTimeZone($a_timezone));
356 $date =
new DateTime($format);
358 }
catch (Exception $ex) {
361 return ($date instanceof DateTime)
371 public function setDate($a_date,
int $a_format): void
373 $this->dt_obj =
null;
382 $this->dt_obj =
new DateTime(
'@' . $a_date);
383 $this->dt_obj->setTimezone(
new DateTimeZone($this->getTimeZoneIdentifier()));
384 }
catch (Exception $ex) {
385 $message =
'Cannot parse date: ' . $a_date .
' with format ' . $a_format;
392 $matches = preg_match(
393 '/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)$/i',
398 $this->log->warning(
'Cannot parse date: ' . $a_date);
399 $this->log->warning(print_r($matches,
true));
404 $tz_id = (isset($d_parts[9]) && $d_parts[9] ===
'Z')
406 : $this->getTimeZoneIdentifier();
407 $this->dt_obj = $this->parsePartsToDate(
421 $this->dt_obj =
new DateTime($a_date,
new DateTimeZone(
'UTC'));
422 }
catch (Exception $ex) {
423 $this->log->warning(
'Cannot parse date : ' . $a_date);
430 $this->dt_obj = $this->parsePartsToDate(
431 (
int) $a_date[
'year'],
432 (
int) $a_date[
'mon'],
433 (
int) $a_date[
'mday'],
434 (
int) $a_date[
'hours'],
435 (
int) $a_date[
'minutes'],
436 (
int) ($a_date[
'seconds'] ?? 0),
437 $this->getTimeZoneIdentifier()
442 if (!preg_match(
"/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date, $d_parts)) {
443 $this->log->warning(
'Cannot parse date: ' . $a_date);
446 $this->dt_obj = $this->parsePartsToDate(
453 $this->getTimeZoneIdentifier()
462 $this->dt_obj = DateTime::createFromFormat(
465 new DateTimeZone($this->getTimeZoneIdentifier())
477 public function get(
int $a_format,
string $a_format_str =
'',
string $a_tz =
'')
479 if ($this->isNull()) {
483 $timezone = $this->default_timezone;
488 $this->log->warning(
'Invalid timezone given. Timezone: ' . $a_tz);
491 $out_date = clone($this->dt_obj);
492 $out_date->setTimezone(
new DateTimeZone($timezone->
getIdentifier()));
498 $date = $this->getUnixTime();
502 $date = $out_date->format(
'Y-m-d');
506 $date = $out_date->format(
'Y-m-d H:i:s');
510 $date = $out_date->format($a_format_str);
515 'seconds' => (
int) $out_date->format(
's')
517 'minutes' => (
int) $out_date->format(
'i')
519 'hours' => (
int) $out_date->format(
'G')
521 'mday' => (
int) $out_date->format(
'j')
523 'wday' => (
int) $out_date->format(
'w')
525 'mon' => (
int) $out_date->format(
'n')
527 'year' => (
int) $out_date->format(
'Y')
529 'yday' => (
int) $out_date->format(
'z')
531 'weekday' => $out_date->format(
'l')
533 'month' => $out_date->format(
'F')
535 'isoday' => (
int) $out_date->format(
'N')
540 $date = $out_date->format(
'c');
544 $date = $out_date->format(
'YmdHis');
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
@classDescription Date and time handling
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)
setDate($a_date, int $a_format)
Set date.
isNull()
Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)
__construct($a_date=null, int $a_format=0, string $a_tz='')
Create new date object.
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 _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.
static _equals(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
Check if two date are equal.
__toString()
to string for date time objects Output is user time zone @access public
switchTimeZone(string $a_timezone_identifier='')
Switch timezone.
increment(string $a_type, int $a_count=1)
ilTimeZone $default_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.
Component logger with individual log levels by component id.
Class for TimeZone exceptions.
This class offers methods for timezone handling.
static _getInstance(string $a_tz='')
get instance by timezone
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))