You are here

protected function DateObject::setGranularityFromTime in Date 7.2

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

Determines the granularity of a date based on the constructor's arguments.

Parameters

string $time: A date string.

bool $tz: TRUE if the date has a timezone, FALSE otherwise.

1 call to DateObject::setGranularityFromTime()
DateObject::__construct in date_api/date_api.module
Constructs a date object.

File

date_api/date_api.module, line 572
This module will make the date API available to other modules.

Class

DateObject
Extend PHP DateTime class.

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');
  }
}