ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  $ilUser = $DIC['ilUser'];
134 
135  if ($date->isNull()) {
136  return self::getLanguage()->txt('no_date');
137  }
138 
139  $has_time = !is_a($date, 'ilDate');
140 
141  // Converting pure dates to user timezone might return wrong dates
142  if ($has_time) {
143  $date_info = $date->get(IL_CAL_FKT_GETDATE, '', $ilUser->getTimeZone());
144  } else {
145  $date_info = $date->get(IL_CAL_FKT_GETDATE, '', 'UTC');
146  }
147 
148  if (!$a_skip_day) {
149  $sep = ", ";
150  if (self::isToday($date) and self::useRelativeDates()) {
151  $date_str = self::getLanguage()->txt('today');
152  } elseif (self::isTomorrow($date) and self::useRelativeDates()) {
153  $date_str = self::getLanguage()->txt('tomorrow');
154  } elseif (self::isYesterday($date) and self::useRelativeDates()) {
155  $date_str = self::getLanguage()->txt('yesterday');
156  } else {
157  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
158  $date_str = "";
159  if ($a_include_wd) {
160  $date_str = $lng->txt(self::$weekdays[$date->get(IL_CAL_FKT_DATE, 'w')]) . ", ";
161  }
162  $date_str .= $date->get(IL_CAL_FKT_DATE, 'd') . '. ' .
163  ilCalendarUtil::_numericMonthToString($date_info['mon'], false) . ' ' .
164  $date_info['year'];
165  }
166  } else {
167  $sep = "";
168  }
169 
170  if (!$has_time) {
171  return $date_str;
172  }
173 
174  $sec = ($include_seconds)
175  ? ":s"
176  : "";
177 
178  switch ($ilUser->getTimeFormat()) {
180  return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'H:i'.$sec, $ilUser->getTimeZone());
181 
183  return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'g:ia'.$sec, $ilUser->getTimeZone());
184  }
185  }
186 
199  public static function formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day = false)
200  {
201  global $DIC;
202 
203  $ilUser = $DIC['ilUser'];
204 
205  $has_time = !is_a($start, 'ilDate');
206 
207  // Same day
208  if (ilDateTime::_equals($start, $end, IL_CAL_DAY, $ilUser->getTimeZone())) {
209  if (!$has_time) {
210  return self::formatDate($start);
211  } else {
212  $date_str = "";
213  $sep = "";
214  if (!$a_skip_starting_day) {
215  $date_str = self::formatDate(
216  new ilDate($start->get(IL_CAL_DATE, '', $ilUser->getTimeZone()), IL_CAL_DATE)
217  );
218  $sep = ", ";
219  }
220 
221  // $start == $end
222  if (ilDateTime::_equals($start, $end)) {
223  switch ($ilUser->getTimeFormat()) {
225  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
226 
228  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'h:i a', $ilUser->getTimeZone());
229  }
230  } else {
231  switch ($ilUser->getTimeFormat()) {
233  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone()) . ' - ' .
234  $end->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
235 
237  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone()) . ' - ' .
238  $end->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
239  }
240  }
241  }
242  }
243  // Different days
244  return self::formatDate($start, $a_skip_starting_day) . ' - ' . self::formatDate($end);
245  }
246 
247 
248 
257  public static function isToday(ilDateTime $date)
258  {
259  global $DIC;
260 
261  $ilUser = $DIC['ilUser'];
262 
263  if (!is_object(self::$today)) {
264  self::$today = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
265  }
266  return ilDateTime::_equals(self::$today, $date, IL_CAL_DAY, $ilUser->getTimeZone());
267  }
268 
277  public static function isYesterday(ilDateTime $date)
278  {
279  global $DIC;
280 
281  $ilUser = $DIC['ilUser'];
282 
283  if (!is_object(self::$yesterday)) {
284  self::$yesterday = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
285  self::$yesterday->increment(IL_CAL_DAY, -1);
286  }
287 
288  return ilDateTime::_equals(self::$yesterday, $date, IL_CAL_DAY, $ilUser->getTimeZone());
289  }
290 
299  public static function isTomorrow(ilDateTime $date)
300  {
301  global $DIC;
302 
303  $ilUser = $DIC['ilUser'];
304 
305  if (!is_object(self::$tomorrow)) {
306  self::$tomorrow = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
307  self::$tomorrow->increment(IL_CAL_DAY, 1);
308  }
309 
310  return ilDateTime::_equals(self::$tomorrow, $date, IL_CAL_DAY, $ilUser->getTimeZone());
311  }
312 
322  public static function secondsToString($seconds, $force_with_seconds = false, $a_lng = null)
323  {
324  global $DIC;
325 
326  $lng = $DIC['lng'];
327 
328  if ($a_lng) {
329  $lng = $a_lng;
330  }
331 
332  $seconds = $seconds ? $seconds : 0;
333 
334  // #13625
335  if ($seconds > 0) {
336  $days = floor($seconds / 86400);
337  $rest = $seconds % 86400;
338 
339  $hours = floor($rest / 3600);
340  $rest = $rest % 3600;
341 
342  $minutes = floor($rest / 60);
343  $seconds = $rest % 60;
344  } else {
345  $days = ceil($seconds / 86400);
346  $rest = $seconds % 86400;
347 
348  $hours = ceil($rest / 3600);
349  $rest = $rest % 3600;
350 
351  $minutes = ceil($rest / 60);
352  $seconds = $rest % 60;
353  }
354 
355  if ($days) {
356  $message = $days . ' ' . ($days == 1 ? $lng->txt('day') : $lng->txt('days'));
357  }
358  if ($hours) {
359  if ($message) {
360  $message .= ' ';
361  }
362  $message .= ($hours . ' ' . ($hours == 1 ? $lng->txt('hour') : $lng->txt('hours')));
363  }
364  if ($minutes) {
365  if ($message) {
366  $message .= ' ';
367  }
368  $message .= ($minutes . ' ' . ($minutes == 1 ? $lng->txt('minute') : $lng->txt('minutes')));
369  }
370  if ($force_with_seconds && $seconds) {
371  if ($message) {
372  $message .= ' ';
373  }
374  $message .= ($seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds')));
375  }
376  if (!$days and !$hours and !$minutes) {
377  return $seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds'));
378  } else {
379  return $message;
380  }
381  }
382 }
$rest
Definition: goto.php:46
global $DIC
Definition: saml.php:7
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
$start
Definition: bench.php:8
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.
catch(Exception $e) $message
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.
const IL_CAL_FKT_DATE
Date and time handling
$ilUser
Definition: imgupload.php:18
get($a_format, $a_format_str='', $a_tz='')
get formatted date
increment($a_type, $a_count=1)
increment
const IL_CAL_FKT_GETDATE
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
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)