ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DateFormat.php
Go to the documentation of this file.
1<?php
18declare(strict_types=1);
19
21
27{
28 public const DOT = '.';
29 public const COMMA = ',';
30 public const DASH = '-';
31 public const SLASH = '/';
32 public const SPACE = ' ';
33 public const DAY = 'd';
34 public const DAY_ORDINAL = 'jS';
35 public const WEEKDAY = 'l';
36 public const WEEKDAY_SHORT = 'D';
37 public const WEEK = 'W';
38 public const MONTH = 'm';
39 public const MONTH_SPELLED = 'F';
40 public const MONTH_SPELLED_SHORT = 'M';
41 public const YEAR = 'Y';
42 public const YEAR_TWO_DIG = 'y';
43 public const HOURS12 = 'h';
44 public const HOURS24 = 'H';
45 public const MINUTES = 'i';
46 public const SECONDS = 's';
47 public const MERIDIEM = 'a';
48 public const COLON = ':';
49
50 public const TOKENS = [
72 ];
73
75 protected array $format = [];
76
77 public function __construct(array $format)
78 {
79 $this->validateFormatElelements($format);
80 $this->format = $format;
81 }
82
83 public function validateFormatElelements(array $format): void
84 {
85 foreach ($format as $entry) {
86 if (!in_array($entry, self::TOKENS, true)) {
87 throw new \InvalidArgumentException("not a valid token for date-format", 1);
88 }
89 }
90 }
91
96 public function toArray(): array
97 {
98 return $this->format;
99 }
100
104 public function toString(): string
105 {
106 return implode('', $this->format);
107 }
108
109 public function __toString(): string
110 {
111 return $this->toString();
112 }
113
114 public function applyTo(\DateTimeImmutable $datetime): string
115 {
116 return $datetime->format($this->toString());
117 }
118}
$datetime
A Date Format provides a format definition akin to PHP's date formatting options, but stores the sing...
Definition: DateFormat.php:27
toArray()
Get the elements of the format as array.
Definition: DateFormat.php:96
toString()
Get the format as string.
Definition: DateFormat.php:104
validateFormatElelements(array $format)
Definition: DateFormat.php:83
applyTo(\DateTimeImmutable $datetime)
Definition: DateFormat.php:114