ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDatePresentation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public static bool $use_relative_dates = true;
29 private static ?ilLanguage $lang = null;
30 public static ?ilDateTime $today = null;
31 public static ?ilDateTime $tomorrow = null;
32 public static ?ilDateTime $yesterday = null;
33 protected static array $weekdays = [
34 0 => 'Su_short',
35 1 => 'Mo_short',
36 2 => 'Tu_short',
37 3 => 'We_short',
38 4 => 'Th_short',
39 5 => 'Fr_short',
40 6 => 'Sa_short'
41 ];
42
46 public static function setUseRelativeDates(bool $a_status): void
47 {
48 self::$use_relative_dates = $a_status;
49 }
50
51 public static function useRelativeDates(): bool
52 {
54 }
55
56 public static function setLanguage(ilLanguage $a_lng): void
57 {
58 self::$lang = $a_lng;
59 }
60
61 public static function getLanguage(): ilLanguage
62 {
63 global $DIC;
64
65 $lng = $DIC->language();
66
67 return self::$lang ?: $lng;
68 }
69
73 public static function resetToDefaults(): void
74 {
75 global $DIC;
76
77 $lng = $DIC->language();
80 }
81
82 public static function formatDate(
83 ilDateTime $date,
84 bool $a_skip_day = false,
85 bool $a_include_wd = false,
86 bool $include_seconds = false,
87 ?ilObjUser $user = null,
88 ): string {
89 global $DIC;
90 if ($user) {
91 $lng = new ilLanguage($user->getLanguage());
92 } else {
93 $user = $DIC->user();
95 }
96
97 $lng->loadLanguageModule('dateplaner');
98
99 if ($date->isNull()) {
100 return $lng->txt('no_date');
101 }
102
103 $has_time = !$date instanceof ilDate;
104
105 // Converting pure dates to user timezone might return wrong dates
106 $date_info = [];
107 if ($has_time) {
108 $date_info = $date->get(IL_CAL_FKT_GETDATE, '', $user->getTimeZone());
109 } else {
110 $date_info = $date->get(IL_CAL_FKT_GETDATE, '', 'UTC');
111 }
112
113 $date_str = '';
114 if (!$a_skip_day) {
115 $sep = ', ';
116 if (self::isToday($date) && self::useRelativeDates()) {
117 $date_str = $lng->txt('today');
118 } elseif (self::isTomorrow($date) && self::useRelativeDates()) {
119 $date_str = $lng->txt('tomorrow');
120 } elseif (self::isYesterday($date) && self::useRelativeDates()) {
121 $date_str = $lng->txt('yesterday');
122 } else {
123 $date_str = '';
124 if ($a_include_wd) {
125 $date_str = $lng->txt(self::$weekdays[$date_info['wday']]) . ', ';
126 }
127 $date_str .= $date_info['mday'] . '. ' .
129 $date_info['mon'],
130 false,
131 $lng
132 ) . ' ' .
133 $date_info['year'];
134 }
135 } else {
136 $sep = '';
137 }
138
139 if (!$has_time) {
140 return $date_str;
141 }
142
143 $sec = ($include_seconds)
144 ? ':s'
145 : '';
146
147 switch ($user->getTimeFormat()) {
149 return $date_str . $sep .
150 $date->get(
152 'H:i' . $sec,
153 $user->getTimeZone()
154 );
156 return $date_str . $sep .
157 $date->get(
159 'g:ia' . $sec,
160 $user->getTimeZone()
161 );
162 }
163
164 return '';
165 }
166
174 public static function formatPeriod(
175 ilDateTime $start,
176 ilDateTime $end,
177 bool $a_skip_starting_day = false,
178 ?ilObjUser $user = null
179 ): string {
180 global $DIC;
181 if (!$user) {
182 $user = $DIC->user();
183 }
184 $has_time = !$start instanceof ilDate;
185
186 // Same day
187 if (ilDateTime::_equals($start, $end, IL_CAL_DAY, $user->getTimeZone())) {
188 if (!$has_time) {
189 return self::formatDate(
190 $start,
191 false,
192 false,
193 false,
194 $user
195 );
196 }
197 $date_str = '';
198 $sep = '';
199 if (!$a_skip_starting_day) {
200 $date_str = self::formatDate(
201 new ilDate(
202 $start->get(IL_CAL_DATE, '', $user->getTimeZone()),
204 ),
205 false,
206 false,
207 false,
208 $user
209 );
210 $sep = ', ';
211 }
212
213 if (ilDateTime::_equals($start, $end)) {
214 switch ($user->getTimeFormat()) {
216 return $date_str . $sep .
217 $start->get(
219 'H:i',
220 $user->getTimeZone()
221 );
223 return $date_str . $sep .
224 $start->get(
226 'h:i a',
227 $user->getTimeZone()
228 );
229 }
230 } else {
231 switch ($user->getTimeFormat()) {
233 return $date_str . $sep .
234 $start->get(
236 'H:i',
237 $user->getTimeZone()
238 ) . ' - ' .
239 $end->get(IL_CAL_FKT_DATE, 'H:i', $user->getTimeZone());
240
242 return $date_str . $sep .
243 $start->get(
245 'g:ia',
246 $user->getTimeZone()
247 ) . ' - ' .
248 $end->get(IL_CAL_FKT_DATE, 'g:ia', $user->getTimeZone());
249 }
250 }
251 }
252
253 // Different days
254 return
255 self::formatDate(
256 $start,
257 $a_skip_starting_day,
258 false,
259 false,
260 $user
261 ) . ' - ' .
262 self::formatDate(
263 $end,
264 false,
265 false,
266 false,
267 $user
268 );
269 }
270
274 public static function isToday(ilDateTime $date): bool
275 {
276 global $DIC;
277
278 $ilUser = $DIC['ilUser'];
279
280 if (!is_object(self::$today)) {
281 self::$today = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
282 }
283
284 return ilDateTime::_equals(self::$today, $date, IL_CAL_DAY, $ilUser->getTimeZone());
285 }
286
290 public static function isYesterday(ilDateTime $date): bool
291 {
292 global $DIC;
293
294 $ilUser = $DIC['ilUser'];
295 if (!is_object(self::$yesterday)) {
296 self::$yesterday = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
297 self::$yesterday->increment(IL_CAL_DAY, -1);
298 }
299
300 return ilDateTime::_equals(self::$yesterday, $date, IL_CAL_DAY, $ilUser->getTimeZone());
301 }
302
306 public static function isTomorrow(ilDateTime $date): bool
307 {
308 global $DIC;
309
310 $ilUser = $DIC['ilUser'];
311 if (!is_object(self::$tomorrow)) {
312 self::$tomorrow = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
313 self::$tomorrow->increment(IL_CAL_DAY, 1);
314 }
315
316 return ilDateTime::_equals(self::$tomorrow, $date, IL_CAL_DAY, $ilUser->getTimeZone());
317 }
318
323 public static function secondsToString(
324 int $seconds,
325 bool $force_with_seconds = false,
326 ?ilLanguage $a_lng = null
327 ): string {
328 global $DIC;
329
330 $lng = $DIC['lng'];
331 $message = null;
332
333 if ($a_lng) {
334 $lng = $a_lng;
335 }
336
337 $seconds = $seconds ?: 0;
338
339 // #13625
340 if ($seconds > 0) {
341 $days = floor($seconds / 86400);
342 $rest = $seconds % 86400;
343
344 $hours = floor($rest / 3600);
345 $rest %= 3600;
346
347 $minutes = floor($rest / 60);
348 $seconds = $rest % 60;
349 } else {
350 $days = ceil($seconds / 86400);
351 $rest = $seconds % 86400;
352
353 $hours = ceil($rest / 3600);
354 $rest %= 3600;
355
356 $minutes = ceil($rest / 60);
357 $seconds = $rest % 60;
358 }
359
360 if ($days) {
361 $message = $days . ' ' . ($days == 1 ? $lng->txt('day') : $lng->txt('days'));
362 }
363 if ($hours) {
364 if ($message) {
365 $message .= ' ';
366 }
367 $message .= ($hours . ' ' . ($hours == 1 ? $lng->txt('hour') : $lng->txt('hours')));
368 }
369 if ($minutes) {
370 if ($message) {
371 $message .= ' ';
372 }
373 $message .= ($minutes . ' ' . ($minutes == 1 ? $lng->txt('minute') : $lng->txt('minutes')));
374 }
375 if ($force_with_seconds && $seconds) {
376 if ($message) {
377 $message .= ' ';
378 }
379 $message .= ($seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds')));
380 }
381 if (!$days && !$hours && !$minutes) {
382 return $seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds'));
383 }
384
385 return $message;
386 }
387}
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
const IL_CAL_DAY
static _numericMonthToString(int $a_month, bool $a_long=true, ?ilLanguage $lng=null)
numeric month to string
Class for date presentation.
static isTomorrow(ilDateTime $date)
Check if date is tomorrow.
static isYesterday(ilDateTime $date)
Check if date is yesterday.
static resetToDefaults()
reset to defaults
static ilDateTime $yesterday
static isToday(ilDateTime $date)
Check if date is "today".
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.
static setLanguage(ilLanguage $a_lng)
static secondsToString(int $seconds, bool $force_with_seconds=false, ?ilLanguage $a_lng=null)
converts seconds to string: Long: 7 days 4 hour(s) ...
@classDescription Date and time handling
isNull()
Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)
static _equals(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
Check if two date are equal.
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
Class for single dates.
language handling
User class.
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
$lang
Definition: xapiexit.php:25
$message
Definition: xapiexit.php:31