ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
ilDateTime Class Reference

Date and time handling More...

+ Inheritance diagram for ilDateTime:
+ Collaboration diagram for ilDateTime:

Public Member Functions

 __construct ($a_date=null, $a_format=0, $a_tz= '')
 Create new date object.
 isNull ()
 Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)
 switchTimeZone ($a_timezone_identifier= '')
 Switch timezone.
 getTimeZoneIdentifier ()
 get timezone identifier
 increment ($a_type, $a_count=1)
 increment
 getUnixTime ()
 get unix time
 getUTCOffset ()
 get UTC offset
 setDate ($a_date, $a_format)
 set date
 get ($a_format, $a_format_str= '', $a_tz= '')
 get formatted date
 __toString ()
 to string

Static Public Member Functions

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.
static _equals (ilDateTime $start, ilDateTime $end, $a_compare_field= '', $a_tz= '')
 Check if two date are equal.
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.

Data Fields

const YEAR = 'year'
const MONTH = 'month'
const WEEK = 'week'
const DAY = 'day'
const HOUR = 'hour'
const MINUTE = 'minute'

Protected Attributes

 $log
 $timezone = null
 $default_timezone = null
 $unix = 0

Detailed Description

Date and time handling

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om
Version
$Id$

Definition at line 31 of file class.ilDateTime.php.

Constructor & Destructor Documentation

ilDateTime::__construct (   $a_date = null,
  $a_format = 0,
  $a_tz = '' 
)

Create new date object.

public

Parameters
mixedinteger string following the format given as the second parameter
intformat of date presentation
@throwsilDateTimeException

Definition at line 59 of file class.ilDateTime.php.

References $ilLog, ilTimeZone\_getInstance(), IL_CAL_UNIX, and setDate().

{
global $ilLog;
$this->log = $ilLog;
try
{
$this->timezone = ilTimeZone::_getInstance($a_tz);
$this->default_timezone = ilTimeZone::_getInstance('');
if(!$a_date)
{
$this->setDate(0,IL_CAL_UNIX);
}
else
{
$this->setDate($a_date,$a_format);
}
}
catch(ilTimeZoneException $exc)
{
$this->log->write(__METHOD__.': '.$exc->getMessage());
throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_tz);
}
}

+ Here is the call graph for this function:

Member Function Documentation

ilDateTime::__toString ( )

to string

public

Parameters
@return

Definition at line 496 of file class.ilDateTime.php.

References IL_CAL_DATETIME.

{
return $this->get(IL_CAL_DATETIME,'','UTC').'<br>';
}
static ilDateTime::_after ( ilDateTime  $start,
ilDateTime  $end,
  $a_compare_field = '',
  $a_tz = '' 
)
static

compare two dates and check start is after end This method does not consider tz offsets.

So you have to take care that both dates are defined in the the same timezone

public

Parameters
objectilDateTime
objectilDateTime
stringfield used for comparison. E.g IL_CAL_YEAR checks if start is one or more years after than end
stringtimezone

Definition at line 205 of file class.ilDateTime.php.

References get(), IL_CAL_DAY, IL_CAL_FKT_DATE, IL_CAL_MONTH, IL_CAL_UNIX, and IL_CAL_YEAR.

Referenced by ilObjGroupAccess\_registrationEnabled(), ilCalendarRecurrenceCalculator\applyLimits(), ilCalendarRecurrenceCalculator\calculateDateList(), ilGroupRegistrationGUI\fillRegistrationPeriod(), ilCourseRegistrationGUI\fillRegistrationPeriod(), ilPageObject\getActive(), ilECSCategoryMappingRule\matchesValue(), and ilECSCategoryMappingRule\validate().

{
switch($a_compare_field)
{
return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
case IL_CAL_DAY:
return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
case '':
default:
return $start->get(IL_CAL_UNIX) > $end->get(IL_CAL_UNIX);
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilDateTime::_before ( ilDateTime  $start,
ilDateTime  $end,
  $a_compare_field = '',
  $a_tz = '' 
)
static

compare two dates and check start is before end This method does not consider tz offsets.

So you have to take care that both dates are defined in the the same timezone

public

Parameters
objectilDateTime
objectilDateTime
stringfield used for comparison. E.g IL_CAL_YEAR checks if start is one or more years earlier than end
stringtimezone
Returns
bool

Definition at line 142 of file class.ilDateTime.php.

References get(), IL_CAL_DAY, IL_CAL_FKT_DATE, IL_CAL_MONTH, IL_CAL_UNIX, and IL_CAL_YEAR.

Referenced by ilObjGroupAccess\_registrationEnabled(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarBlockGUI\addMiniMonth(), ilCalendarRecurrenceCalculator\applyLimits(), ilCalendarRecurrenceCalculator\calculateDateList(), ilGroupRegistrationGUI\fillRegistrationPeriod(), ilCourseRegistrationGUI\fillRegistrationPeriod(), ilPageObject\getActive(), ilECSCategoryMappingRule\matchesValue(), ilCalendarRecurrenceCalculator\optimizeStartingTime(), ilCalendarMonthGUI\show(), ilObjGroup\validate(), and ilCalendarEntry\validate().

{
switch($a_compare_field)
{
return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
case IL_CAL_DAY:
return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
case '':
default:
return $start->get(IL_CAL_UNIX) < $end->get(IL_CAL_UNIX);
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static ilDateTime::_equals ( ilDateTime  $start,
ilDateTime  $end,
  $a_compare_field = '',
  $a_tz = '' 
)
static

Check if two date are equal.

public

Parameters
objectilDateTime
objectilDateTime
stringfield used for comparison. E.g IL_CAL_YEAR checks if start is the same years than end
stringtimzone

Definition at line 173 of file class.ilDateTime.php.

References get(), IL_CAL_DAY, IL_CAL_FKT_DATE, IL_CAL_MONTH, IL_CAL_UNIX, and IL_CAL_YEAR.

Referenced by ilCalendarUtil\_isToday(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarBlockGUI\addMiniMonth(), ilCalendarRecurrenceCalculator\calculateDateList(), ilSubItemSelectionTableGUI\fillRow(), ilDatePresentation\formatPeriod(), ilDatePresentation\isToday(), ilDatePresentation\isTomorrow(), ilDatePresentation\isYesterday(), ilDateList\removeByDAY(), and ilCalendarMonthGUI\show().

{
switch($a_compare_field)
{
return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
case IL_CAL_DAY:
return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
case '':
default:
return $start->get(IL_CAL_UNIX) == $end->get(IL_CAL_UNIX);
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilDateTime::get (   $a_format,
  $a_format_str = '',
  $a_tz = '' 
)

get formatted date

public

Parameters
intformat type
stringformat string
stringa specific timezone

Definition at line 428 of file class.ilDateTime.php.

References $default_timezone, $timezone, ilTimeZone\_getInstance(), getUnixTime(), IL_CAL_DATE, IL_CAL_DATETIME, IL_CAL_FKT_DATE, IL_CAL_FKT_GETDATE, IL_CAL_TIMESTAMP, and IL_CAL_UNIX.

Referenced by _after(), _before(), _equals(), ilDateDurationInputGUI\checkInput(), ilDatePresentation\formatDate(), ilDatePresentation\formatPeriod(), ilCalendarSchedule\getByDay(), ilObjUserGUI\getValues(), ilCalendarRecurrenceCalculator\getYearWeekDays(), ilCalendarRegistration\isRegistered(), and ilCalendarRegistration\unregister().

{
if($a_tz)
{
try
{
}
catch(ilTimeZoneException $exc)
{
$this->log->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_tz);
}
}
else
{
#$timezone = $this->timezone;
}
switch($a_format)
{
$date = $this->getUnixTime();
break;
$timezone->switchTZ();
$date = date('Y-m-d',$this->getUnixTime());
$timezone->restoreTZ();
break;
$timezone->switchTZ();
$date = date('Y-m-d H:i:s',$this->getUnixTime());
$timezone->restoreTZ();
break;
$timezone->switchTZ();
$date = date($a_format_str,$this->getUnixTime());
$timezone->restoreTZ();
break;
$timezone->switchTZ();
$date = getdate($this->getUnixTime());
$timezone->restoreTZ();
// add iso 8601 week day number (Sunday = 7)
$date['isoday'] = $date['wday'] == 0 ? 7 : $date['wday'];
break;
$timezone->switchTZ();
$date = date('YmdHis',$this->getUnixTime());
$timezone->restoreTZ();
break;
}
return $date;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilDateTime::getTimeZoneIdentifier ( )

get timezone identifier

public

Definition at line 123 of file class.ilDateTime.php.

{
return $this->timezone->getIdentifier();
}
ilDateTime::getUnixTime ( )

get unix time

public

Definition at line 294 of file class.ilDateTime.php.

References $unix.

Referenced by get().

{
return $this->unix;
}

+ Here is the caller graph for this function:

ilDateTime::getUTCOffset ( )

get UTC offset

public

Returns
offset to utc in seconds

Definition at line 306 of file class.ilDateTime.php.

{
$this->timezone->switchTZ();
// TODO: This is wrong: calculate UTC offset of given date
$offset = mktime(0,0,0,2,1,1970) - gmmktime(0,0,0,2,1,1970);
$this->timezone->restoreTZ();
return $offset;
}
ilDateTime::increment (   $a_type,
  $a_count = 1 
)

increment

public

Parameters
inttype
intcount

Definition at line 233 of file class.ilDateTime.php.

References $d, $unix, IL_CAL_FKT_DATE, and IL_CAL_UNIX.

Referenced by ilCalendarSchedule\getByDay(), ilCalendarSchedule\initPeriod(), ilDatePresentation\isTomorrow(), and ilDatePresentation\isYesterday().

{
$count_str = $a_count > 0 ? ('+'.$a_count.' ') : ($a_count.' ');
$this->timezone->switchTZ();
switch($a_type)
{
case self::YEAR:
$this->unix = strtotime($count_str.'year',$this->unix);
break;
case self::MONTH:
// strtotime fails in the following case:
// 2008-03-31 (+1 month) => 2008-05-01
// In that case, we substract the new month day
$old_day_of_month = $this->get(IL_CAL_FKT_DATE,'j');
$this->unix = strtotime($count_str.'month',$this->unix);
// TODO: Fix monthly calculations.
/*
$new_day_of_month = $this->get(IL_CAL_FKT_DATE,'j');
if($new_day_of_month != $old_day_of_month)
{
$this->unix = $this->increment(IL_CAL_DAY,$new_day_of_month * -1);
}
*/
break;
case self::WEEK:
$this->unix = strtotime($count_str.'week',$this->unix);
break;
case self::DAY:
$this->unix = strtotime($count_str.'day',$this->unix);
break;
case self::HOUR:
$this->unix = strtotime($count_str.'hour',$this->unix);
break;
case self::MINUTE:
$this->unix = strtotime($count_str.'minute',$this->unix);
$d = new ilDateTime($this->unix,IL_CAL_UNIX);
break;
}
$this->timezone->restoreTZ();
return $this->unix;
}

+ Here is the caller graph for this function:

ilDateTime::isNull ( )

Check if a date is null (Datetime == '0000-00-00 00:00:00', unixtime == 0,...)

Returns
bool

Definition at line 91 of file class.ilDateTime.php.

Referenced by ilDatePresentation\formatDate().

{
return $this->unix ? false : true;
}

+ Here is the caller graph for this function:

ilDateTime::setDate (   $a_date,
  $a_format 
)

set date

public

Parameters
mixeddate
intformat

Definition at line 323 of file class.ilDateTime.php.

References $timezone, $unix, ilTimeZone\_getInstance(), IL_CAL_DATE, IL_CAL_DATETIME, IL_CAL_FKT_GETDATE, IL_CAL_TIMESTAMP, and IL_CAL_UNIX.

Referenced by __construct(), ilUserProfile\addStandardFieldsToForm(), ilObjExerciseGUI\getAssignmentValues(), and ilObjCourseGUI\initForm().

{
switch($a_format)
{
$this->unix = $a_date;
break;
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)
{
$this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
throw new ilDateTimeException('Cannot parse date.');
}
$this->timezone->switchTZ();
$this->unix = mktime(
isset($d_parts[5]) ? $d_parts[5] : 0,
isset($d_parts[6]) ? $d_parts[6] : 0,
isset($d_parts[7]) ? $d_parts[7] : 0,
$d_parts[2],
$d_parts[3],
$d_parts[1]);
if($d_parts[0] == '0000-00-00 00:00:00')
{
$this->unix = 0;
}
$this->timezone->restoreTZ();
break;
// Pure dates are not timezone sensible.
$timezone->switchTZ();
$unix = strtotime($a_date);
$timezone->restoreTZ();
if($unix === false)
{
$this->log->write(__METHOD__.': Cannot parse date : '.$a_date);
$this->unix = 0;
return false;
}
$this->unix = $unix;
break;
if (!isset($a_date['seconds']))
{
$a_date['seconds'] = false;
}
// Format like getdate parameters
$this->timezone->switchTZ();
$this->unix = mktime(
$a_date['hours'],
$a_date['minutes'],
$a_date['seconds'],
$a_date['mon'],
$a_date['mday'],
$a_date['year']);
$this->timezone->restoreTZ();
// TODO: choose better error handling
if(!$a_date['year'])
{
$this->unix = 0;
}
break;
if(preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) == false)
{
$this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
throw new ilDateTimeException('Cannot parse date.');
}
$this->timezone->switchTZ();
$this->unix = mktime(
isset($d_parts[4]) ? $d_parts[4] : 0,
isset($d_parts[5]) ? $d_parts[5] : 0,
isset($d_parts[6]) ? $d_parts[6] : 0,
$d_parts[2],
$d_parts[3],
$d_parts[1]);
if($d_parts[0] == '00000000000000' or
$d_parts[0] == '00000000')
{
$this->unix = 0;
}
$this->timezone->restoreTZ();
break;
}
return true;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilDateTime::switchTimeZone (   $a_timezone_identifier = '')

Switch timezone.

public

Parameters
stringPHP timezone identifier
Exceptions
ilDateTimeException

Definition at line 103 of file class.ilDateTime.php.

References ilTimeZone\_getInstance().

Referenced by ilCalendarRecurrenceCalculator\adjustTimeZones().

{
try
{
$this->timezone = ilTimeZone::_getInstance($a_timezone_identifier);
return true;
}
{
$this->log->write('Unsupported timezone given: '.$a_timezone_identifier);
throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_timezone_identifier);
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Field Documentation

ilDateTime::$default_timezone = null
protected

Definition at line 43 of file class.ilDateTime.php.

Referenced by get().

ilDateTime::$log
protected

Definition at line 40 of file class.ilDateTime.php.

ilDateTime::$timezone = null
protected

Definition at line 42 of file class.ilDateTime.php.

Referenced by get(), and setDate().

ilDateTime::$unix = 0
protected

Definition at line 45 of file class.ilDateTime.php.

Referenced by getUnixTime(), increment(), and setDate().

const ilDateTime::HOUR = 'hour'

Definition at line 37 of file class.ilDateTime.php.

const ilDateTime::MINUTE = 'minute'

Definition at line 38 of file class.ilDateTime.php.

Referenced by ilConsultationHoursGUI\createAppointments().

const ilDateTime::YEAR = 'year'

The documentation for this class was generated from the following file: