You are here

protected function DateObject::setGranularityFromTime in Date 7

Same name and namespace in other branches
  1. 7.3 date_api/date_api.module \DateObject::setGranularityFromTime()
  2. 7.2 date_api/date_api.module \DateObject::setGranularityFromTime()

Protected function to find the granularity given by the arguments to the constructor.

1 call to DateObject::setGranularityFromTime()
DateObject::__construct in date_api/date_api.module
Overridden constructor.

File

date_api/date_api.module, line 324
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Class

DateObject
Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.

Code

protected function setGranularityFromTime($time, $tz) {
  $this->granularity = array();
  $temp = date_parse($time);

  // Special case for "now"
  if ($time == 'now') {
    $this->granularity = array(
      'year',
      'month',
      'day',
      'hour',
      'minute',
      'second',
    );
  }
  else {

    // This PHP date_parse() method currently doesn't have resolution down to seconds, so if
    // there is some time, all will be set.
    foreach (self::$allgranularity as $g) {
      if (isset($temp[$g]) && is_numeric($temp[$g]) || $g == 'timezone' && (isset($temp['zone_type']) && $temp['zone_type'] > 0)) {
        $this->granularity[] = $g;
      }
    }
  }
  if ($tz) {
    $this
      ->addGranularity('timezone');
  }
}