ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilDatePresentation.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once('./Services/Calendar/classes/class.ilDate.php');
25 include_once('./Services/Calendar/classes/class.ilCalendarSettings.php');
26 
36 {
37  public static $use_relative_dates = true;
38  private static $lang = null;
39 
40  public static $today = null;
41  public static $tomorrow = null;
42  public static $yesterday = null;
43 
44  protected static $weekdays = array(
45  0 => "Su_short",
46  1 => "Mo_short",
47  2 => "Tu_short",
48  3 => "We_short",
49  4 => "Th_short",
50  5 => "Fr_short",
51  6 => "Sa_short"
52  );
53 
60  public static function setUseRelativeDates($a_status)
61  {
62  self::$use_relative_dates = $a_status;
63  }
64 
71  public static function useRelativeDates()
72  {
73  return self::$use_relative_dates;
74  }
75 
82  public static function setLanguage($a_lng)
83  {
84  self::$lang = $a_lng;
85  }
86 
93  public static function getLanguage()
94  {
95  global $DIC;
96 
97  $lng = $DIC['lng'];
98 
99  return self::$lang ? self::$lang : $lng;
100  }
101 
108  public static function resetToDefaults()
109  {
110  global $DIC;
111 
112  $lng = $DIC['lng'];
113 
114  self::setLanguage($lng);
115  self::setUseRelativeDates(true);
116  }
117 
118 
119 
127  public static function formatDate(ilDateTime $date, $a_skip_day = false, $a_include_wd = false,
128  $include_seconds = false)
129  {
130  global $DIC;
131 
132  $lng = $DIC['lng'];
133  $lng->loadLanguageModule('dateplaner');
134  $ilUser = $DIC['ilUser'];
135 
136  if ($date->isNull()) {
137  return self::getLanguage()->txt('no_date');
138  }
139 
140  $has_time = !is_a($date, 'ilDate');
141 
142  // Converting pure dates to user timezone might return wrong dates
143  $date_info = [];
144  if ($has_time) {
145  $date_info = $date->get(IL_CAL_FKT_GETDATE, '', $ilUser->getTimeZone());
146  } else {
147  $date_info = $date->get(IL_CAL_FKT_GETDATE, '', 'UTC');
148  }
149 
150  if (!$a_skip_day) {
151  $sep = ", ";
152  if (self::isToday($date) and self::useRelativeDates()) {
153  $date_str = self::getLanguage()->txt('today');
154  } elseif (self::isTomorrow($date) and self::useRelativeDates()) {
155  $date_str = self::getLanguage()->txt('tomorrow');
156  } elseif (self::isYesterday($date) and self::useRelativeDates()) {
157  $date_str = self::getLanguage()->txt('yesterday');
158  } else {
159  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
160  $date_str = "";
161  if ($a_include_wd) {
162  $date_str = $lng->txt(self::$weekdays[$date_info['wday']]) . ", ";
163  }
164  $date_str .= $date_info['mday'] . '. ' .
165  ilCalendarUtil::_numericMonthToString($date_info['mon'], false) . ' ' .
166  $date_info['year'];
167  }
168  } else {
169  $sep = "";
170  }
171 
172  if (!$has_time) {
173  return $date_str;
174  }
175 
176  $sec = ($include_seconds)
177  ? ":s"
178  : "";
179 
180  switch ($ilUser->getTimeFormat()) {
182  return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'H:i'.$sec, $ilUser->getTimeZone());
183 
185  return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'g:ia'.$sec, $ilUser->getTimeZone());
186  }
187  }
188 
201  public static function formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day = false)
202  {
203  global $DIC;
204 
205  $ilUser = $DIC['ilUser'];
206 
207  $has_time = !is_a($start, 'ilDate');
208 
209  // Same day
210  if (ilDateTime::_equals($start, $end, IL_CAL_DAY, $ilUser->getTimeZone())) {
211  if (!$has_time) {
212  return self::formatDate($start);
213  } else {
214  $date_str = "";
215  $sep = "";
216  if (!$a_skip_starting_day) {
217  $date_str = self::formatDate(
218  new ilDate($start->get(IL_CAL_DATE, '', $ilUser->getTimeZone()), IL_CAL_DATE)
219  );
220  $sep = ", ";
221  }
222 
223  // $start == $end
224  if (ilDateTime::_equals($start, $end)) {
225  switch ($ilUser->getTimeFormat()) {
227  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
228 
230  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'h:i a', $ilUser->getTimeZone());
231  }
232  } else {
233  switch ($ilUser->getTimeFormat()) {
235  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone()) . ' - ' .
236  $end->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
237 
239  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone()) . ' - ' .
240  $end->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
241  }
242  }
243  }
244  }
245  // Different days
246  return self::formatDate($start, $a_skip_starting_day) . ' - ' . self::formatDate($end);
247  }
248 
249 
250 
259  public static function isToday(ilDateTime $date)
260  {
261  global $DIC;
262 
263  $ilUser = $DIC['ilUser'];
264 
265  if (!is_object(self::$today)) {
266  self::$today = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
267  }
268  return ilDateTime::_equals(self::$today, $date, IL_CAL_DAY, $ilUser->getTimeZone());
269  }
270 
279  public static function isYesterday(ilDateTime $date)
280  {
281  global $DIC;
282 
283  $ilUser = $DIC['ilUser'];
284 
285  if (!is_object(self::$yesterday)) {
286  self::$yesterday = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
287  self::$yesterday->increment(IL_CAL_DAY, -1);
288  }
289 
290  return ilDateTime::_equals(self::$yesterday, $date, IL_CAL_DAY, $ilUser->getTimeZone());
291  }
292 
301  public static function isTomorrow(ilDateTime $date)
302  {
303  global $DIC;
304 
305  $ilUser = $DIC['ilUser'];
306 
307  if (!is_object(self::$tomorrow)) {
308  self::$tomorrow = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
309  self::$tomorrow->increment(IL_CAL_DAY, 1);
310  }
311 
312  return ilDateTime::_equals(self::$tomorrow, $date, IL_CAL_DAY, $ilUser->getTimeZone());
313  }
314 
324  public static function secondsToString($seconds, $force_with_seconds = false, $a_lng = null)
325  {
326  global $DIC;
327 
328  $lng = $DIC['lng'];
329 
330  if ($a_lng) {
331  $lng = $a_lng;
332  }
333 
334  $seconds = $seconds ? $seconds : 0;
335 
336  // #13625
337  if ($seconds > 0) {
338  $days = floor($seconds / 86400);
339  $rest = $seconds % 86400;
340 
341  $hours = floor($rest / 3600);
342  $rest = $rest % 3600;
343 
344  $minutes = floor($rest / 60);
345  $seconds = $rest % 60;
346  } else {
347  $days = ceil($seconds / 86400);
348  $rest = $seconds % 86400;
349 
350  $hours = ceil($rest / 3600);
351  $rest = $rest % 3600;
352 
353  $minutes = ceil($rest / 60);
354  $seconds = $rest % 60;
355  }
356 
357  if ($days) {
358  $message = $days . ' ' . ($days == 1 ? $lng->txt('day') : $lng->txt('days'));
359  }
360  if ($hours) {
361  if ($message) {
362  $message .= ' ';
363  }
364  $message .= ($hours . ' ' . ($hours == 1 ? $lng->txt('hour') : $lng->txt('hours')));
365  }
366  if ($minutes) {
367  if ($message) {
368  $message .= ' ';
369  }
370  $message .= ($minutes . ' ' . ($minutes == 1 ? $lng->txt('minute') : $lng->txt('minutes')));
371  }
372  if ($force_with_seconds && $seconds) {
373  if ($message) {
374  $message .= ' ';
375  }
376  $message .= ($seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds')));
377  }
378  if (!$days and !$hours and !$minutes) {
379  return $seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds'));
380  } else {
381  return $message;
382  }
383  }
384 }
$rest
Definition: goto.php:48
static resetToDefaults()
reset to defaults
static setUseRelativeDates($a_status)
set use relative dates
static isToday(ilDateTime $date)
Check if date is "today".
const IL_CAL_UNIX
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
static _numericMonthToString($a_month, $a_long=true)
numeric month to string
static useRelativeDates()
check if relative dates are used
static setLanguage($a_lng)
set language
const IL_CAL_DAY
Class for date presentation.
static _equals(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
Check if two date are equal.
static secondsToString($seconds, $force_with_seconds=false, $a_lng=null)
converts seconds to string: Long: 7 days 4 hour(s) ...
Class for single dates.
$lng
static isYesterday(ilDateTime $date)
Check if date is yesterday.
global $DIC
Definition: goto.php:24
const IL_CAL_FKT_DATE
get($a_format, $a_format_str='', $a_tz='')
get formatted date
const IL_CAL_FKT_GETDATE
$lang
Definition: xapiexit.php:8
static isTomorrow(ilDateTime $date)
Check if date is tomorrow.
const IL_CAL_DATE
static formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day=false)
Format a period of two date Shows: 14.
static getLanguage()
set language
$message
Definition: xapiexit.php:14
$ilUser
Definition: imgupload.php:18
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)