5 include_once(
'Services/Calendar/classes/class.ilDateTimeException.php');
6 include_once(
'Services/Calendar/classes/class.ilTimeZone.php');
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 define(
'IL_CAL_ISO_8601',7);
17 define(
'IL_CAL_YEAR',
'year');
18 define(
'IL_CAL_MONTH',
'month');
19 define(
'IL_CAL_WEEK',
'week');
20 define(
'IL_CAL_DAY',
'day');
21 define(
'IL_CAL_HOUR',
'hour');
60 public function __construct($a_date = null,$a_format = 0,$a_tz =
'')
71 $this->
setDate($a_date,$a_format);
75 $this->log->write(__METHOD__.
': '.$exc->getMessage());
90 return array(
'timezone',
'default_timezone',
'dt_obj');
107 return !($this->dt_obj instanceof DateTime);
126 $this->log->write(
'Unsupported timezone given: '.$a_timezone_identifier);
127 throw new ilDateTimeException(
'Unsupported timezone given. Timezone: '.$a_timezone_identifier);
139 return $this->
timezone->getIdentifier();
163 switch($a_compare_field)
176 return $start->dt_obj < $end->dt_obj;
199 switch($a_compare_field)
212 return $start->dt_obj == $end->dt_obj;
236 switch($a_compare_field)
249 return $start->dt_obj > $end->dt_obj;
284 $sub = ($a_count < 0);
285 $count_str = abs($a_count);
290 $count_str .=
'year';
294 $count_str .=
'month';
298 $count_str .=
'week';
306 $count_str .=
'hour';
310 $count_str .=
'minute';
314 $interval = date_interval_create_from_date_string($count_str);
317 $this->dt_obj->add($interval);
321 $this->dt_obj->sub($interval);
338 return $this->dt_obj->getTimestamp();
353 $offset = $this->dt_obj->getOffset();
361 protected function parsePartsToDate($a_year, $a_month, $a_day, $a_hour = null, $a_min = null, $a_sec = null, $a_timezone = null)
363 $a_year = (int)$a_year;
364 $a_month = (int)$a_month;
365 $a_day = (int)$a_day;
374 $a_hour = (int)$a_hour;
375 $a_min = (int)$a_min;
376 $a_sec = (int)$a_sec;
378 $format = $a_year.
'-'.$a_month.
'-'.$a_day;
382 $format .=
' '.(int)$a_hour.
':'.(
int)$a_min.
':'.(int)$a_sec;
390 $date =
new DateTime($format,
new DateTimeZone($a_timezone));
394 $date =
new DateTime($format);
401 return ($date instanceof DateTime)
416 $this->dt_obj = null;
428 $this->dt_obj =
new DateTime(
'@'.$a_date);
432 $this->log->write(__METHOD__.
': Cannot parse date: '.$a_date);
438 $matches = 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);
441 $this->log->write(__METHOD__.
': Cannot parse date: '.$a_date);
442 $this->log->write(__METHOD__.
': '.print_r($matches,
true));
443 $this->log->logStack();
447 $tz_id = ($d_parts[9] ==
'Z')
450 $this->dt_obj = $this->
parsePartsToDate($d_parts[1], $d_parts[2], $d_parts[3],
451 $d_parts[5], $d_parts[6], $d_parts[7], $tz_id);
458 $this->dt_obj =
new DateTime($a_date,
new DateTimeZone(
'UTC'));
462 $this->log->write(__METHOD__.
': Cannot parse date : '.$a_date);
469 $this->dt_obj = $this->
parsePartsToDate($a_date[
'year'], $a_date[
'mon'], $a_date[
'mday'],
470 $a_date[
'hours'], $a_date[
'minutes'], $a_date[
'seconds']);
474 if(preg_match(
"/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) ==
false)
476 $this->log->write(__METHOD__.
': Cannot parse date: '.$a_date);
479 $this->dt_obj = $this->
parsePartsToDate($d_parts[1], $d_parts[2], $d_parts[3],
480 $d_parts[4], $d_parts[5], $d_parts[6]);
484 $this->dt_obj = DateTime::createFromFormat(DateTime::ISO8601, $a_date);
491 $this->dt_obj->setTimeZone(
new DateTimeZone($this->default_timezone->getIdentifier()));
505 public function get($a_format,$a_format_str =
'',$a_tz =
'')
520 $this->log->write(__METHOD__.
': Invalid timezone given. Timezone: '.$a_tz);
528 $out_date = clone($this->dt_obj);
529 $out_date->setTimeZone(
new DateTimeZone(
$timezone->getIdentifier()));
539 $date = $out_date->format(
'Y-m-d');
543 $date = $out_date->format(
'Y-m-d H:i:s');
547 $date = $out_date->format($a_format_str);
552 'seconds' => (
int)$out_date->format(
's')
553 ,
'minutes' => (int)$out_date->format(
'i')
554 ,
'hours' => (int)$out_date->format(
'G')
555 ,
'mday' => (int)$out_date->format(
'j')
556 ,
'wday' => (int)$out_date->format(
'w')
557 ,
'mon' => (int)$out_date->format(
'n')
558 ,
'year' => (int)$out_date->format(
'Y')
559 ,
'yday' => (int)$out_date->format(
'z')
560 ,
'weekday' => $out_date->format(
'l')
561 ,
'month' => $out_date->format(
'F')
562 ,
'isoday' => (int)$out_date->format(
'N')
567 $date = $out_date->format(
'c');
571 $date = $out_date->format(
'YmdHis');
__construct($a_date=null, $a_format=0, $a_tz='')
Create new date object.
static _after(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
static _before(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
switchTimeZone($a_timezone_identifier='')
Switch timezone.
Class for DateTime exceptions.
__toString()
to string for date time objects Output is user time zone
getTimeZoneIdentifier()
get timezone identifier
static _equals(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
Check if two date are equal.
getUTCOffset()
get UTC offset
Class for TimeZone exceptions.
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.
static _getInstance($a_tz='')
get instance by timezone
getUnixTime()
get unix time
setDate($a_date, $a_format)
set date
parsePartsToDate($a_year, $a_month, $a_day, $a_hour=null, $a_min=null, $a_sec=null, $a_timezone=null)
static _within(ilDateTime $dt, ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
Check whether an date is within a date duration given by start and end.
isNull()
Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)