ILIAS  release_8 Revision v8.24
class.ilDatePresentation.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/*
5 +-----------------------------------------------------------------------------+
6 | ILIAS open source |
7 +-----------------------------------------------------------------------------+
8 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
9 | |
10 | This program is free software; you can redistribute it and/or |
11 | modify it under the terms of the GNU General Public License |
12 | as published by the Free Software Foundation; either version 2 |
13 | of the License, or (at your option) any later version. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License |
21 | along with this program; if not, write to the Free Software |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 +-----------------------------------------------------------------------------+
24*/
25
32{
33 public static bool $use_relative_dates = true;
34 private static ?ilLanguage $lang = null;
35
36 public static ?ilDateTime $today = null;
37 public static ?ilDateTime $tomorrow = null;
38 public static ?ilDateTime $yesterday = null;
39
40 protected static array $weekdays = array(
41 0 => "Su_short",
42 1 => "Mo_short",
43 2 => "Tu_short",
44 3 => "We_short",
45 4 => "Th_short",
46 5 => "Fr_short",
47 6 => "Sa_short"
48 );
49
53 public static function setUseRelativeDates(bool $a_status): void
54 {
55 self::$use_relative_dates = $a_status;
56 }
57
58 public static function useRelativeDates(): bool
59 {
61 }
62
63 public static function setLanguage(ilLanguage $a_lng): void
64 {
65 self::$lang = $a_lng;
66 }
67
68 public static function getLanguage(): ilLanguage
69 {
70 global $DIC;
71
72 $lng = $DIC->language();
73 return self::$lang ?: $lng;
74 }
75
79 public static function resetToDefaults()
80 {
81 global $DIC;
82
83 $lng = $DIC->language();
86 }
87
88 public static function formatDate(ilDateTime $date, bool $a_skip_day = false, bool $a_include_wd = false, bool $include_seconds = false): string
89 {
90 global $DIC;
91
92 $lng = $DIC['lng'];
93 $lng->loadLanguageModule('dateplaner');
94 $ilUser = $DIC['ilUser'];
95
96 if ($date->isNull()) {
97 return self::getLanguage()->txt('no_date');
98 }
99
100 $has_time = !is_a($date, 'ilDate');
101
102 // Converting pure dates to user timezone might return wrong dates
103 $date_info = [];
104 if ($has_time) {
105 $date_info = $date->get(IL_CAL_FKT_GETDATE, '', $ilUser->getTimeZone());
106 } else {
107 $date_info = $date->get(IL_CAL_FKT_GETDATE, '', 'UTC');
108 }
109
110 $date_str = '';
111 if (!$a_skip_day) {
112 $sep = ", ";
113 if (self::isToday($date) and self::useRelativeDates()) {
114 $date_str = self::getLanguage()->txt('today');
115 } elseif (self::isTomorrow($date) and self::useRelativeDates()) {
116 $date_str = self::getLanguage()->txt('tomorrow');
117 } elseif (self::isYesterday($date) and self::useRelativeDates()) {
118 $date_str = self::getLanguage()->txt('yesterday');
119 } else {
120 $date_str = "";
121 if ($a_include_wd) {
122 $date_str = $lng->txt(self::$weekdays[$date_info['wday']]) . ", ";
123 }
124 $date_str .= $date_info['mday'] . '. ' .
125 ilCalendarUtil::_numericMonthToString($date_info['mon'], false) . ' ' .
126 $date_info['year'];
127 }
128 } else {
129 $sep = "";
130 }
131
132 if (!$has_time) {
133 return $date_str;
134 }
135
136 $sec = ($include_seconds)
137 ? ":s"
138 : "";
139
140 switch ($ilUser->getTimeFormat()) {
142 return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'H:i' . $sec, $ilUser->getTimeZone());
143
145 return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'g:ia' . $sec, $ilUser->getTimeZone());
146 }
147 return '';
148 }
149
157 public static function formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day = false): string
158 {
159 global $DIC;
160
161 $ilUser = $DIC['ilUser'];
162 $has_time = !is_a($start, 'ilDate');
163
164 // Same day
165 if (ilDateTime::_equals($start, $end, IL_CAL_DAY, $ilUser->getTimeZone())) {
166 if (!$has_time) {
167 return self::formatDate($start);
168 } else {
169 $date_str = "";
170 $sep = "";
171 if (!$a_skip_starting_day) {
172 $date_str = self::formatDate(
173 new ilDate($start->get(IL_CAL_DATE, '', $ilUser->getTimeZone()), IL_CAL_DATE)
174 );
175 $sep = ", ";
176 }
177
178 // $start == $end
179 if (ilDateTime::_equals($start, $end)) {
180 switch ($ilUser->getTimeFormat()) {
182 return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
183
185 return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'h:i a', $ilUser->getTimeZone());
186 }
187 } else {
188 switch ($ilUser->getTimeFormat()) {
190 return $date_str . $sep . $start->get(
192 'H:i',
193 $ilUser->getTimeZone()
194 ) . ' - ' .
195 $end->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
196
198 return $date_str . $sep . $start->get(
200 'g:ia',
201 $ilUser->getTimeZone()
202 ) . ' - ' .
203 $end->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
204 }
205 }
206 }
207 }
208 // Different days
209 return self::formatDate($start, $a_skip_starting_day) . ' - ' . self::formatDate($end);
210 }
211
215 public static function isToday(ilDateTime $date): bool
216 {
217 global $DIC;
218
219 $ilUser = $DIC['ilUser'];
220
221 if (!is_object(self::$today)) {
222 self::$today = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
223 }
224 return ilDateTime::_equals(self::$today, $date, IL_CAL_DAY, $ilUser->getTimeZone());
225 }
226
230 public static function isYesterday(ilDateTime $date): bool
231 {
232 global $DIC;
233
234 $ilUser = $DIC['ilUser'];
235 if (!is_object(self::$yesterday)) {
236 self::$yesterday = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
237 self::$yesterday->increment(IL_CAL_DAY, -1);
238 }
239
240 return ilDateTime::_equals(self::$yesterday, $date, IL_CAL_DAY, $ilUser->getTimeZone());
241 }
242
246 public static function isTomorrow(ilDateTime $date): bool
247 {
248 global $DIC;
249
250 $ilUser = $DIC['ilUser'];
251 if (!is_object(self::$tomorrow)) {
252 self::$tomorrow = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
253 self::$tomorrow->increment(IL_CAL_DAY, 1);
254 }
255
256 return ilDateTime::_equals(self::$tomorrow, $date, IL_CAL_DAY, $ilUser->getTimeZone());
257 }
258
263 public static function secondsToString(
264 int $seconds,
265 bool $force_with_seconds = false,
266 ?ilLanguage $a_lng = null
267 ): string {
268 global $DIC;
269
270 $lng = $DIC['lng'];
271 $message = null;
272
273 if ($a_lng) {
274 $lng = $a_lng;
275 }
276
277 $seconds = $seconds ?: 0;
278
279 // #13625
280 if ($seconds > 0) {
281 $days = floor($seconds / 86400);
282 $rest = $seconds % 86400;
283
284 $hours = floor($rest / 3600);
285 $rest = $rest % 3600;
286
287 $minutes = floor($rest / 60);
288 $seconds = $rest % 60;
289 } else {
290 $days = ceil($seconds / 86400);
291 $rest = $seconds % 86400;
292
293 $hours = ceil($rest / 3600);
294 $rest = $rest % 3600;
295
296 $minutes = ceil($rest / 60);
297 $seconds = $rest % 60;
298 }
299
300 if ($days) {
301 $message = $days . ' ' . ($days == 1 ? $lng->txt('day') : $lng->txt('days'));
302 }
303 if ($hours) {
304 if ($message) {
305 $message .= ' ';
306 }
307 $message .= ($hours . ' ' . ($hours == 1 ? $lng->txt('hour') : $lng->txt('hours')));
308 }
309 if ($minutes) {
310 if ($message) {
311 $message .= ' ';
312 }
313 $message .= ($minutes . ' ' . ($minutes == 1 ? $lng->txt('minute') : $lng->txt('minutes')));
314 }
315 if ($force_with_seconds && $seconds) {
316 if ($message) {
317 $message .= ' ';
318 }
319 $message .= ($seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds')));
320 }
321 if (!$days and !$hours and !$minutes) {
322 return $seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds'));
323 } else {
324 return $message;
325 }
326 }
327}
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)
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 formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
static ilDateTime $yesterday
static isToday(ilDateTime $date)
Check if date is "today".
static setUseRelativeDates(bool $a_status)
set use relative dates
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) ...
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
@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
global $DIC
Definition: feed.php:28
$rest
Definition: goto.php:49
$ilUser
Definition: imgupload.php:34
$lng
$lang
Definition: xapiexit.php:26
$message
Definition: xapiexit.php:32