Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
8.82% |
9 / 102 |
|
7.69% |
1 / 13 |
CRAP | |
0.00% |
0 / 1 |
DateUtils | |
8.82% |
9 / 102 |
|
7.69% |
1 / 13 |
1191.86 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentDateInFormat | |
53.33% |
8 / 15 |
|
0.00% |
0 / 1 |
14.50 | |||
getIsoToday | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
sanitizeDatetimeValue | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
sanitizeDateValue | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
getIsoNow | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isoDateTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
isoDate | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
compactDate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
compactTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
olzDate | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
getTimestamp | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
formatDateTimeRange | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | |
3 | namespace Olz\Utils; |
4 | |
5 | class DateUtils { |
6 | use WithUtilsTrait; |
7 | |
8 | public const WEEKDAYS_SHORT_DE = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"]; |
9 | public const WEEKDAYS_LONG_DE = ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]; |
10 | public const MONTHS_SHORT_DE = ["Jan.", "Feb.", "März", "April", "Mai", "Juni", "Juli", "Aug.", "Sept.", "Okt.", "Nov.", "Dez."]; |
11 | public const MONTHS_LONG_DE = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]; |
12 | |
13 | public function __construct(protected ?string $date = null) { |
14 | } |
15 | |
16 | public function getCurrentDateInFormat(string $format): string { |
17 | if ($this->date !== null) { |
18 | if ($this->date === 'live') { |
19 | return date($format); |
20 | } |
21 | return date($format, @strtotime($this->date) ?: null); |
22 | } |
23 | $class_name = $this->envUtils()->getDateUtilsClassName(); |
24 | $class_args = $this->envUtils()->getDateUtilsClassArgs(); |
25 | |
26 | if ($class_name == 'FixedDateUtils') { |
27 | $fixed_date = $class_args[0]; |
28 | $fixed_date = is_int($fixed_date) |
29 | ? $fixed_date |
30 | : (@strtotime($fixed_date) ?: null); |
31 | return date($format, $fixed_date); |
32 | } |
33 | if ($class_name == 'LiveDateUtils') { |
34 | return date($format); |
35 | } |
36 | throw new \Exception("Date class must be FixedDateUtils or LiveDateUtils, was: {$class_name}"); |
37 | } |
38 | |
39 | public function getIsoToday(): string { |
40 | return $this->getCurrentDateInFormat('Y-m-d'); |
41 | } |
42 | |
43 | public function sanitizeDatetimeValue(string|\DateTime|null $value): ?\DateTime { |
44 | if ($value == null) { |
45 | return null; |
46 | } |
47 | if ($value instanceof \DateTime) { |
48 | return $value; |
49 | } |
50 | $res = preg_match('/[0-9]+\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/', $value); |
51 | if (!$res) { |
52 | throw new \Exception("Invalid datetime: {$value}", 1); |
53 | } |
54 | $datetime = \DateTime::createFromFormat('Y-m-d H:i:s', $value); |
55 | if (!$datetime) { |
56 | throw new \Exception("Invalid datetime: {$value}", 1); |
57 | } |
58 | return $datetime; |
59 | } |
60 | |
61 | public function sanitizeDateValue(string|\DateTime|null $value): ?\DateTime { |
62 | if ($value == null) { |
63 | return null; |
64 | } |
65 | if ($value instanceof \DateTime) { |
66 | return $value; |
67 | } |
68 | $res = preg_match('/[0-9]+\-[0-9]{2}\-[0-9]{2}/', $value); |
69 | if (!$res) { |
70 | throw new \Exception("Invalid datetime: {$value}", 1); |
71 | } |
72 | $datetime = \DateTime::createFromFormat('Y-m-d', $value); |
73 | if (!$datetime) { |
74 | throw new \Exception("Invalid datetime: {$value}", 1); |
75 | } |
76 | return $datetime; |
77 | } |
78 | |
79 | public function getIsoNow(): string { |
80 | return $this->getCurrentDateInFormat('Y-m-d H:i:s'); |
81 | } |
82 | |
83 | public function isoDateTime(string|\DateTime|null $date = null): string { |
84 | $timestamp = $this->getTimestamp($date); |
85 | return date("Y-m-d H:i:s", $timestamp); |
86 | } |
87 | |
88 | public function isoDate(string|\DateTime|null $date = null): string { |
89 | $timestamp = $this->getTimestamp($date); |
90 | return date("Y-m-d", $timestamp); |
91 | } |
92 | |
93 | public function compactDate(string|\DateTime|null $date = null): string { |
94 | return $this->olzDate("W,\xc2\xa0tt.mm.", $date); |
95 | } |
96 | |
97 | public function compactTime(string|\DateTime|null $date = null): string { |
98 | $timestamp = $this->getTimestamp($date); |
99 | return date("H:i", $timestamp); |
100 | } |
101 | |
102 | public function olzDate(string $format, string|\DateTime|null $date = null): string { |
103 | $date = $this->getTimestamp($date); |
104 | |
105 | return str_replace( |
106 | [ |
107 | "tt", |
108 | "t", |
109 | "mm", |
110 | "m", |
111 | "MM", |
112 | "M", |
113 | "xxxxx", |
114 | "jjjj", |
115 | "jj", |
116 | "w", |
117 | "WW", |
118 | "W", |
119 | ], |
120 | [ |
121 | date("d", $date), |
122 | date("j", $date), |
123 | date("m", $date), |
124 | date("n", $date), |
125 | "xxxxx", |
126 | self::MONTHS_SHORT_DE[date("n", $date) - 1], |
127 | self::MONTHS_LONG_DE[date("n", $date) - 1], |
128 | date("Y", $date), |
129 | date("y", $date), |
130 | date("w", $date), |
131 | self::WEEKDAYS_LONG_DE[date("w", $date)], |
132 | self::WEEKDAYS_SHORT_DE[date("w", $date)], |
133 | ], |
134 | $format |
135 | ); |
136 | } |
137 | |
138 | protected function getTimestamp(string|\DateTime|null $date = null): int { |
139 | if ($date == null || $date == '') { |
140 | $date = $this->getIsoNow(); |
141 | } |
142 | if ($date instanceof \DateTime) { |
143 | $date = $date->format(\DateTime::ATOM); |
144 | } |
145 | $timestamp = strtotime($date); |
146 | $this->generalUtils()->checkNotFalse($timestamp, "No timestamp for {$date}"); |
147 | return $timestamp; |
148 | } |
149 | |
150 | public function formatDateTimeRange( |
151 | string $start_date, |
152 | ?string $start_time, |
153 | ?string $end_date, |
154 | ?string $end_time, |
155 | string $format = 'long', |
156 | ): string { |
157 | if (!$end_date) { |
158 | $end_date = $start_date; |
159 | } |
160 | $out = ''; |
161 | // Date |
162 | if ($end_date == $start_date) { |
163 | // Eintägig |
164 | $out = $this->olzDate('WW, t. MM jjjj', $start_date); |
165 | } else { |
166 | $weekday_prefix = $this->olzDate('WW', $start_date).' – '.$this->olzDate('WW', $end_date).', '; |
167 | if ($this->olzDate('m', $start_date) == $this->olzDate('m', $end_date)) { |
168 | // Mehrtägig, innerhalb Monat |
169 | $out = $weekday_prefix.$this->olzDate('t.', $start_date).' – '.$this->olzDate('t. ', $end_date).$this->olzDate('MM jjjj', $start_date); |
170 | } elseif ($this->olzDate('jjjj', $start_date) == $this->olzDate('jjjj', $end_date)) { |
171 | // Mehrtägig, innerhalb Jahr |
172 | $out = $weekday_prefix.$this->olzDate('t. MM', $start_date).' – '.$this->olzDate('t. MM jjjj', $end_date); |
173 | } else { |
174 | // Mehrtägig, jahresübergreifend |
175 | $out = $weekday_prefix.$this->olzDate('t. MM jjjj', $start_date).' – '.$this->olzDate('t. MM jjjj', $end_date); |
176 | } |
177 | } |
178 | // Time |
179 | if ($start_time) { |
180 | $out .= ' '.date('H:i', strtotime($start_time) ?: null); |
181 | if ($end_time) { |
182 | $out .= ' – '.date('H:i', strtotime($end_time) ?: null); |
183 | } |
184 | } |
185 | return $out; |
186 | } |
187 | } |