ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
50  public static function setUseRelativeDates($a_status)
51  {
52  self::$use_relative_dates = $a_status;
53  }
54 
61  public static function useRelativeDates()
62  {
63  return self::$use_relative_dates;
64  }
65 
72  public static function setLanguage($a_lng)
73  {
74  self::$lang = $a_lng;
75  }
76 
83  public static function getLanguage()
84  {
85  global $lng;
86 
87  return self::$lang ? self::$lang : $lng;
88  }
89 
96  public static function resetToDefaults()
97  {
98  global $lng;
99 
100  self::setLanguage($lng);
101  self::setUseRelativeDates(true);
102  }
103 
104 
105 
113  public static function formatDate(ilDateTime $date)
114  {
115  global $lng,$ilUser;
116 
117  if($date->isNull())
118  {
119  return self::getLanguage()->txt('no_date');
120  }
121 
122  $has_time = !is_a($date,'ilDate');
123 
124  // Converting pure dates to user timezone might return wrong dates
125  if($has_time)
126  {
127  $date_info = $date->get(IL_CAL_FKT_GETDATE,'',$ilUser->getTimeZone());
128  }
129  else
130  {
131  $date_info = $date->get(IL_CAL_FKT_GETDATE,'','UTC');
132  }
133 
134 
135  if(self::isToday($date) and self::useRelativeDates())
136  {
137  $date_str = self::getLanguage()->txt('today');
138  }
139  elseif(self::isTomorrow($date) and self::useRelativeDates())
140  {
141  $date_str = self::getLanguage()->txt('tomorrow');
142  }
143  elseif(self::isYesterday($date) and self::useRelativeDates())
144  {
145  $date_str = self::getLanguage()->txt('yesterday');
146  }
147  else
148  {
149  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
150  $date_str = $date->get(IL_CAL_FKT_DATE,'d').'. '.
151  ilCalendarUtil::_numericMonthToString($date_info['mon'],false).' '.
152  $date_info['year'];
153  }
154 
155  if(!$has_time)
156  {
157  return $date_str;
158  }
159 
160  switch($ilUser->getTimeFormat())
161  {
163  return $date_str.', '.$date->get(IL_CAL_FKT_DATE,'H:i',$ilUser->getTimeZone());
164 
166  return $date_str.', '.$date->get(IL_CAL_FKT_DATE,'g:ia',$ilUser->getTimeZone());
167  }
168  }
169 
182  public static function formatPeriod(ilDateTime $start,ilDateTime $end)
183  {
184  global $ilUser;
185 
186  $has_time = !is_a($start,'ilDate');
187 
188  // Same day
189  if(ilDateTime::_equals($start,$end,IL_CAL_DAY,$ilUser->getTimeZone()))
190  {
191  if(!$has_time)
192  {
193  return self::formatDate($start);
194  }
195  else
196  {
197  $date_str = self::formatDate(
198  new ilDate($start->get(IL_CAL_DATE,'',$ilUser->getTimeZone()),IL_CAL_DATE));
199 
200  // $start == $end
201  if(ilDateTime::_equals($start,$end))
202  {
203  switch($ilUser->getTimeFormat())
204  {
206  return $date_str.', '.$start->get(IL_CAL_FKT_DATE,'H:i',$ilUser->getTimeZone());
207 
209  return $date_str.', '.$start->get(IL_CAL_FKT_DATE,'h:i a',$ilUser->getTimeZone());
210  }
211  }
212  else
213  {
214  switch($ilUser->getTimeFormat())
215  {
217  return $date_str.', '.$start->get(IL_CAL_FKT_DATE,'H:i',$ilUser->getTimeZone()).' - '.
218  $end->get(IL_CAL_FKT_DATE,'H:i',$ilUser->getTimeZone());
219 
221  return $date_str.', '.$start->get(IL_CAL_FKT_DATE,'g:ia',$ilUser->getTimeZone()).' - '.
222  $end->get(IL_CAL_FKT_DATE,'g:ia',$ilUser->getTimeZone());
223  }
224  }
225  }
226  }
227  // Different days
228  return self::formatDate($start).' - '.self::formatDate($end);
229  }
230 
231 
232 
241  public static function isToday(ilDateTime $date)
242  {
243  global $ilUser;
244 
245  if(!is_object(self::$today))
246  {
247  self::$today = new ilDateTime(time(),IL_CAL_UNIX,$ilUser->getTimeZone());
248  }
249  return ilDateTime::_equals(self::$today,$date,IL_CAL_DAY,$ilUser->getTimeZone());
250  }
251 
260  public static function isYesterday(ilDateTime $date)
261  {
262  global $ilUser;
263 
264  if(!is_object(self::$yesterday))
265  {
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  {
287  self::$tomorrow = new ilDateTime(time(),IL_CAL_UNIX,$ilUser->getTimeZone());
288  self::$tomorrow->increment(IL_CAL_DAY,1);
289  }
290 
291  return ilDateTime::_equals(self::$tomorrow,$date,IL_CAL_DAY,$ilUser->getTimeZone());
292  }
293 
303  public static function secondsToString($seconds, $force_with_seconds = false, $a_lng = null)
304  {
305  global $lng;
306 
307  if($a_lng)
308  {
309  $lng = $a_lng;
310  }
311 
312  $seconds = $seconds ? $seconds : 0;
313 
314  // #13625
315  if($seconds > 0)
316  {
317  $days = floor($seconds / 86400);
318  $rest = $seconds % 86400;
319 
320  $hours = floor($rest / 3600);
321  $rest = $rest % 3600;
322 
323  $minutes = floor($rest / 60);
324  $seconds = $rest % 60;
325  }
326  else
327  {
328  $days = ceil($seconds / 86400);
329  $rest = $seconds % 86400;
330 
331  $hours = ceil($rest / 3600);
332  $rest = $rest % 3600;
333 
334  $minutes = ceil($rest / 60);
335  $seconds = $rest % 60;
336  }
337 
338  if($days)
339  {
340  $message = $days . ' '. ($days == 1 ? $lng->txt('day') : $lng->txt('days'));
341  }
342  if($hours)
343  {
344  if($message)
345  {
346  $message .= ' ';
347  }
348  $message .= ($hours . ' '. ($hours == 1 ? $lng->txt('hour') : $lng->txt('hours')));
349  }
350  if($minutes)
351  {
352  if($message)
353  {
354  $message .= ' ';
355  }
356  $message .= ($minutes . ' '. ($minutes == 1 ? $lng->txt('minute') : $lng->txt('minutes')));
357  }
358  if($force_with_seconds && $seconds)
359  {
360  if($message)
361  {
362  $message .= ' ';
363  }
364  $message .= ($seconds . ' '. ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds')));
365  }
366  if(!$days and !$hours and !$minutes)
367  {
368  return $seconds .' '. ($seconds == 1 ? $lng->txt('second') : $lng->txt('seconds'));
369  }
370  else
371  {
372  return $message;
373  }
374  }
375 }
376 ?>
$rest
Definition: goto.php:48
static formatPeriod(ilDateTime $start, ilDateTime $end)
Format a period of two date Shows: 14.
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.
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
static formatDate(ilDateTime $date)
Format a date public.
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 getLanguage()
set language
global $lng
Definition: privfeed.php:17
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349
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,...)