ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDateTime.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once('Services/Calendar/classes/class.ilDateTimeException.php');
6 include_once('Services/Calendar/classes/class.ilTimeZone.php');
7 
8 
9 define('IL_CAL_DATETIME',1);
10 define('IL_CAL_DATE',2);
11 define('IL_CAL_UNIX',3);
12 define('IL_CAL_FKT_DATE',4);
13 define('IL_CAL_FKT_GETDATE',5);
14 define('IL_CAL_TIMESTAMP',6);
15 
16 define('IL_CAL_YEAR','year');
17 define('IL_CAL_MONTH','month');
18 define('IL_CAL_WEEK','week');
19 define('IL_CAL_DAY','day');
20 define('IL_CAL_HOUR','hour');
21 
22 
32 {
33  const YEAR = 'year';
34  const MONTH = 'month';
35  const WEEK = 'week';
36  const DAY = 'day';
37  const HOUR = 'hour';
38  const MINUTE = 'minute';
39 
40  protected $log;
41 
42  protected $timezone = null;
43  protected $default_timezone = null;
44 
45  protected $unix = 0;
46 
47 
48 
59  public function __construct($a_date = null,$a_format = 0,$a_tz = '')
60  {
61  global $ilLog;
62 
63  $this->log = $ilLog;
64 
65  try
66  {
67  $this->timezone = ilTimeZone::_getInstance($a_tz);
68  $this->default_timezone = ilTimeZone::_getInstance('');
69 
70  if(!$a_date)
71  {
72  $this->setDate(0,IL_CAL_UNIX);
73  }
74  else
75  {
76  $this->setDate($a_date,$a_format);
77  }
78  }
79  catch(ilTimeZoneException $exc)
80  {
81  $this->log->write(__METHOD__.': '.$exc->getMessage());
82  throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_tz);
83  }
84  }
85 
91  public function isNull()
92  {
93  return $this->unix ? false : true;
94  }
95 
103  public function switchTimeZone($a_timezone_identifier = '')
104  {
105  try
106  {
107  $this->timezone = ilTimeZone::_getInstance($a_timezone_identifier);
108  return true;
109  }
110  catch(ilTimeZoneException $e)
111  {
112  $this->log->write('Unsupported timezone given: '.$a_timezone_identifier);
113  throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_timezone_identifier);
114  }
115  }
116 
123  public function getTimeZoneIdentifier()
124  {
125  return $this->timezone->getIdentifier();
126  }
127 
142  public static function _before(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
143  {
144  switch($a_compare_field)
145  {
146  case IL_CAL_YEAR:
147  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
148 
149  case IL_CAL_MONTH:
150  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
151 
152  case IL_CAL_WEEK:
153  return (int) $start->get(IL_CAL_FKT_DATE,'YW',$a_tz) < $end->get(IL_CAL_FKT_DATE,'YW',$a_tz);
154 
155  case IL_CAL_DAY:
156  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
157 
158  case '':
159  default:
160  return $start->get(IL_CAL_UNIX) < $end->get(IL_CAL_UNIX);
161 
162  }
163  }
164 
176  public static function _equals(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
177  {
178  switch($a_compare_field)
179  {
180  case IL_CAL_YEAR:
181  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
182 
183  case IL_CAL_MONTH:
184  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
185 
186  case IL_CAL_WEEK:
187  return (int) $start->get(IL_CAL_FKT_DATE,'YW',$a_tz) == $end->get(IL_CAL_FKT_DATE,'YW',$a_tz);
188 
189  case IL_CAL_DAY:
190  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
191 
192  case '':
193  default:
194  return $start->get(IL_CAL_UNIX) == $end->get(IL_CAL_UNIX);
195 
196  }
197  }
198 
211  public static function _after(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
212  {
213  switch($a_compare_field)
214  {
215  case IL_CAL_YEAR:
216  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
217 
218  case IL_CAL_MONTH:
219  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
220 
221  case IL_CAL_WEEK:
222  return (int) $start->get(IL_CAL_FKT_DATE,'YW',$a_tz) > $end->get(IL_CAL_FKT_DATE,'YW',$a_tz);
223 
224  case IL_CAL_DAY:
225  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
226 
227  case '':
228  default:
229  return $start->get(IL_CAL_UNIX) > $end->get(IL_CAL_UNIX);
230 
231  }
232  }
233 
242  public static function _within(ilDateTime $dt, ilDateTime $start, ilDateTime $end, $a_compare_field = '', $a_tz = '')
243  {
244  return
245  (ilDateTime::_after($dt, $start,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $start,$a_compare_field,$a_tz)) &&
246  (ilDateTime::_before($dt, $end,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $end,$a_compare_field,$a_tz));
247  }
248 
257  public function increment($a_type,$a_count = 1)
258  {
259  $count_str = $a_count > 0 ? ('+'.$a_count.' ') : ($a_count.' ');
260 
261  $this->timezone->switchTZ();
262  switch($a_type)
263  {
264  case self::YEAR:
265  $this->unix = strtotime($count_str.'year',$this->unix);
266  break;
267 
268  case self::MONTH:
269  $this->unix = strtotime($count_str.'month',$this->unix);
270  break;
271 
272  case self::WEEK:
273  $this->unix = strtotime($count_str.'week',$this->unix);
274  break;
275 
276  case self::DAY:
277  $this->unix = strtotime($count_str.'day',$this->unix);
278  break;
279 
280  case self::HOUR:
281  $this->unix = strtotime($count_str.'hour',$this->unix);
282  break;
283 
284  case self::MINUTE:
285 
286  $this->unix = strtotime($count_str.'minute',$this->unix);
287  $d = new ilDateTime($this->unix,IL_CAL_UNIX);
288 
289 
290  break;
291 
292  }
293  $this->timezone->restoreTZ();
294  return $this->unix;
295  }
296 
303  public function getUnixTime()
304  {
305  return $this->unix;
306  }
307 
308 
315  public function getUTCOffset()
316  {
317  $this->timezone->switchTZ();
318  // TODO: This is wrong: calculate UTC offset of given date
319  $offset = mktime(0,0,0,2,1,1970) - gmmktime(0,0,0,2,1,1970);
320  $this->timezone->restoreTZ();
321  return $offset;
322  }
323 
332  public function setDate($a_date,$a_format)
333  {
334  switch($a_format)
335  {
336  case IL_CAL_UNIX:
337  $this->unix = $a_date;
338  break;
339 
340  case IL_CAL_DATETIME:
341 
342  if(preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)$/i',$a_date,$d_parts) < 1)
343  {
344  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
345  $this->log->logStack();
346  throw new ilDateTimeException('Cannot parse date.');
347  }
348 
349  $this->timezone->switchTZ();
350  $this->unix = mktime(
351  isset($d_parts[5]) ? $d_parts[5] : 0,
352  isset($d_parts[6]) ? $d_parts[6] : 0,
353  isset($d_parts[7]) ? $d_parts[7] : 0,
354  $d_parts[2],
355  $d_parts[3],
356  $d_parts[1]);
357 
358  if($d_parts[0] == '0000-00-00 00:00:00')
359  {
360  $this->unix = 0;
361  }
362 
363  $this->timezone->restoreTZ();
364  break;
365 
366  case IL_CAL_DATE:
367  // Pure dates are not timezone sensible.
369  $timezone->switchTZ();
370  $unix = strtotime($a_date);
371  $timezone->restoreTZ();
372  if($unix === false)
373  {
374  $this->log->write(__METHOD__.': Cannot parse date : '.$a_date);
375  $this->unix = 0;
376  return false;
377  }
378  $this->unix = $unix;
379  break;
380 
381  case IL_CAL_FKT_GETDATE:
382  if (!isset($a_date['seconds']))
383  {
384  $a_date['seconds'] = false;
385  }
386  // Format like getdate parameters
387  $this->timezone->switchTZ();
388  $this->unix = mktime(
389  $a_date['hours'],
390  $a_date['minutes'],
391  $a_date['seconds'],
392  $a_date['mon'],
393  $a_date['mday'],
394  $a_date['year']);
395  $this->timezone->restoreTZ();
396 
397  // TODO: choose better error handling
398  if(!$a_date['year'])
399  {
400  $this->unix = 0;
401  }
402  break;
403 
404  case IL_CAL_TIMESTAMP:
405  if(preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) == false)
406  {
407  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
408  throw new ilDateTimeException('Cannot parse date.');
409  }
410  $this->timezone->switchTZ();
411  $this->unix = mktime(
412  isset($d_parts[4]) ? $d_parts[4] : 0,
413  isset($d_parts[5]) ? $d_parts[5] : 0,
414  isset($d_parts[6]) ? $d_parts[6] : 0,
415  $d_parts[2],
416  $d_parts[3],
417  $d_parts[1]);
418 
419  if($d_parts[0] == '00000000000000' or
420  $d_parts[0] == '00000000')
421  {
422  $this->unix = 0;
423  }
424  $this->timezone->restoreTZ();
425  break;
426  }
427  return true;
428  }
429 
438  public function get($a_format,$a_format_str = '',$a_tz = '')
439  {
440  if($a_tz)
441  {
442  try
443  {
445  }
446  catch(ilTimeZoneException $exc)
447  {
448  $this->log->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_tz);
449  }
450  }
451  else
452  {
453  #$timezone = $this->timezone;
455  }
456 
457  switch($a_format)
458  {
459  case IL_CAL_UNIX:
460  $date = $this->getUnixTime();
461  break;
462 
463  case IL_CAL_DATE:
464  $timezone->switchTZ();
465  $date = date('Y-m-d',$this->getUnixTime());
466  $timezone->restoreTZ();
467  break;
468 
469  case IL_CAL_DATETIME:
470  $timezone->switchTZ();
471  $date = date('Y-m-d H:i:s',$this->getUnixTime());
472  $timezone->restoreTZ();
473  break;
474 
475  case IL_CAL_FKT_DATE:
476  $timezone->switchTZ();
477  $date = date($a_format_str,$this->getUnixTime());
478  $timezone->restoreTZ();
479  break;
480 
481  case IL_CAL_FKT_GETDATE:
482  $timezone->switchTZ();
483  $date = getdate($this->getUnixTime());
484  $timezone->restoreTZ();
485 
486  // add iso 8601 week day number (Sunday = 7)
487  $date['isoday'] = $date['wday'] == 0 ? 7 : $date['wday'];
488  break;
489 
490  case IL_CAL_TIMESTAMP:
491  $timezone->switchTZ();
492  $date = date('YmdHis',$this->getUnixTime());
493  $timezone->restoreTZ();
494  break;
495  }
496  return $date;
497  }
498 
507  public function __toString()
508  {
509  return $this->get(IL_CAL_DATETIME).'<br>';
510  }
511 }
512 ?>