ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $lng;
96 
97  return self::$lang ? self::$lang : $lng;
98  }
99 
106  public static function resetToDefaults()
107  {
108  global $lng;
109 
110  self::setLanguage($lng);
111  self::setUseRelativeDates(true);
112  }
113 
114 
115 
123  public static function formatDate(ilDateTime $date, $a_skip_day = false, $a_include_wd = false)
124  {
125  global $lng,$ilUser;
126 
127  if ($date->isNull()) {
128  return self::getLanguage()->txt('no_date');
129  }
130 
131  $has_time = !is_a($date, 'ilDate');
132 
133  // Converting pure dates to user timezone might return wrong dates
134  if ($has_time) {
135  $date_info = $date->get(IL_CAL_FKT_GETDATE, '', $ilUser->getTimeZone());
136  } else {
137  $date_info = $date->get(IL_CAL_FKT_GETDATE, '', 'UTC');
138  }
139 
140  if (!$a_skip_day) {
141  $sep = ", ";
142  if (self::isToday($date) and self::useRelativeDates()) {
143  $date_str = self::getLanguage()->txt('today');
144  } elseif (self::isTomorrow($date) and self::useRelativeDates()) {
145  $date_str = self::getLanguage()->txt('tomorrow');
146  } elseif (self::isYesterday($date) and self::useRelativeDates()) {
147  $date_str = self::getLanguage()->txt('yesterday');
148  } else {
149  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
150  $date_str = "";
151  if ($a_include_wd) {
152  $date_str = $lng->txt(self::$weekdays[$date->get(IL_CAL_FKT_DATE, 'w')]) . ", ";
153  }
154  $date_str.= $date->get(IL_CAL_FKT_DATE, 'd') . '. ' .
155  ilCalendarUtil::_numericMonthToString($date_info['mon'], false) . ' ' .
156  $date_info['year'];
157  }
158  } else {
159  $sep = "";
160  }
161 
162  if (!$has_time) {
163  return $date_str;
164  }
165 
166  switch ($ilUser->getTimeFormat()) {
168  return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
169 
171  return $date_str . $sep . $date->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
172  }
173  }
174 
187  public static function formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day = false)
188  {
189  global $ilUser;
190 
191  $has_time = !is_a($start, 'ilDate');
192 
193  // Same day
194  if (ilDateTime::_equals($start, $end, IL_CAL_DAY, $ilUser->getTimeZone())) {
195  if (!$has_time) {
196  return self::formatDate($start);
197  } else {
198  $date_str = "";
199  $sep = "";
200  if (!$a_skip_starting_day) {
201  $date_str = self::formatDate(
202  new ilDate($start->get(IL_CAL_DATE, '', $ilUser->getTimeZone()), IL_CAL_DATE)
203  );
204  $sep = ", ";
205  }
206 
207  // $start == $end
208  if (ilDateTime::_equals($start, $end)) {
209  switch ($ilUser->getTimeFormat()) {
211  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
212 
214  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'h:i a', $ilUser->getTimeZone());
215  }
216  } else {
217  switch ($ilUser->getTimeFormat()) {
219  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone()) . ' - ' .
220  $end->get(IL_CAL_FKT_DATE, 'H:i', $ilUser->getTimeZone());
221 
223  return $date_str . $sep . $start->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone()) . ' - ' .
224  $end->get(IL_CAL_FKT_DATE, 'g:ia', $ilUser->getTimeZone());
225  }
226  }
227  }
228  }
229  // Different days
230  return self::formatDate($start, $a_skip_starting_day) . ' - ' . self::formatDate($end);
231  }
232 
233 
234 
243  public static function isToday(ilDateTime $date)
244  {
245  global $ilUser;
246 
247  if (!is_object(self::$today)) {
248  self::$today = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
249  }
250  return ilDateTime::_equals(self::$today, $date, IL_CAL_DAY, $ilUser->getTimeZone());
251  }
252 
261  public static function isYesterday(ilDateTime $date)
262  {
263  global $ilUser;
264 
265  if (!is_object(self::$yesterday)) {
266  self::$yesterday = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
267  self::$yesterday->increment(IL_CAL_DAY, -1);
268  }
269 
270  return ilDateTime::_equals(self::$yesterday, $date, IL_CAL_DAY, $ilUser->getTimeZone());
271  }
272 
281  public static function isTomorrow(ilDateTime $date)
282  {
283  global $ilUser;
284 
285  if (!is_object(self::$tomorrow)) {
286  self::$tomorrow = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
287  self::$tomorrow->increment(IL_CAL_DAY, 1);
288  }
289 
290  return ilDateTime::_equals(self::$tomorrow, $date, IL_CAL_DAY, $ilUser->getTimeZone());
291  }
292 
302  public static function secondsToString($seconds, $force_with_seconds = false, $a_lng = null)
303  {
304  global $lng;
305 
306  if ($a_lng) {
307  $lng = $a_lng;
308  }
309 
310  $seconds = $seconds ? $seconds : 0;
311 
312  // #13625
313  if ($seconds > 0) {
314  $days = floor($seconds / 86400);
315  $rest = $seconds % 86400;
316 
317  $hours = floor($rest / 3600);
318  $rest = $rest % 3600;
319 
320  $minutes = floor($rest / 60);
321  $seconds = $rest % 60;
322  } else {
323  $days = ceil($seconds / 86400);
324  $rest = $seconds % 86400;
325 
326  $hours = ceil($rest / 3600);
327  $rest = $rest % 3600;
328 
329  $minutes = ceil($rest / 60);
330  $seconds = $rest % 60;
331  }
332 
333  if ($days) {
334  $message = $days . ' ' . ($days == 1 ? $lng->txt('day') : $lng->txt('days'));
335  }
336  if ($hours) {
337  if ($message) {
338  $message .= ' ';
339  }
340  $message .= ($hours . ' ' . ($hours == 1 ? $lng->txt('hour') : $lng->txt('hours')));
341  }
342  if ($minutes) {
343  if ($message) {
344  $message .= ' ';
345  }
346  $message .= ($minutes . ' ' . ($minutes == 1 ? $lng->txt('minute') : $lng->txt('minutes')));
347  }
348  if ($force_with_seconds && $seconds) {
349  if ($message) {
350  $message .= ' ';
351  }
352  $message .= ($seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds')));
353  }
354  if (!$days and !$hours and !$minutes) {
355  return $seconds . ' ' . ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds'));
356  } else {
357  return $message;
358  }
359  }
360 }
$rest
Definition: goto.php:46
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false)
Format a date public.
$end
Definition: saml1-acs.php:18
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 _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.
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.
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
Create styles array
The data for the language used.
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
global $lng
Definition: privfeed.php:17
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
isNull()
Check if a date is null (Datetime == &#39;0000-00-00 00:00:00&#39;, unixtime == 0,...)