You are here

public function DateObject::hasGranularity in Date 7.3

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

Checks granularity array for a given entry.

@returns bool TRUE if the date part is present in the date's granularity.

Parameters

array|null $g: An array of date parts. Defaults to NULL.

4 calls to DateObject::hasGranularity()
DateObject::hasTime in date_api/date_api.module
Returns whether this object has time set.
DateObject::merge in date_api/date_api.module
Merges two date objects together using the current date values as defaults.
DateObject::setTimezone in date_api/date_api.module
Sets the time zone for the current date.
DateObject::validGranularity in date_api/date_api.module
Determines if a a date is valid for a given granularity.

File

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

Class

DateObject
Extend PHP DateTime class.

Code

public function hasGranularity($g = NULL) {
  if ($g === NULL) {

    // Just want to know if it has something valid means no lower
    // granularities without higher ones.
    $last = TRUE;
    foreach (self::$allgranularity as $arg) {
      if ($arg == 'timezone') {
        continue;
      }
      if (in_array($arg, $this->granularity) && !$last) {
        return FALSE;
      }
      $last = in_array($arg, $this->granularity);
    }
    return in_array('year', $this->granularity);
  }
  if (is_array($g)) {
    foreach ($g as $gran) {
      if (!in_array($gran, $this->granularity)) {
        return FALSE;
      }
    }
    return TRUE;
  }
  return in_array($g, $this->granularity);
}