You are here

public function DateObject::validGranularity in Date 7.2

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

Determines if a a date is valid for a given granularity.

Parameters

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

bool $flexible: TRUE if the granuliarty is flexible, FALSE otherwise. Defaults to FALSE.

Return value

bool Whether a date is valid for a given granularity.

File

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

Class

DateObject
Extend PHP DateTime class.

Code

public function validGranularity($granularity = NULL, $flexible = FALSE) {
  $true = $this
    ->hasGranularity() && (!$granularity || $flexible || $this
    ->hasGranularity($granularity));
  if (!$true && $granularity) {
    $allowed_values = array(
      'second',
      'minute',
      'hour',
      'day',
      'month',
      'year',
    );
    foreach ((array) $granularity as $part) {
      if (!$this
        ->hasGranularity($part) && in_array($part, $allowed_values)) {
        switch ($part) {
          case 'second':
            $this->errors[$part] = t('The second is missing.');
            break;
          case 'minute':
            $this->errors[$part] = t('The minute is missing.');
            break;
          case 'hour':
            $this->errors[$part] = t('The hour is missing.');
            break;
          case 'day':
            $this->errors[$part] = t('The day is missing.');
            break;
          case 'month':
            $this->errors[$part] = t('The month is missing.');
            break;
          case 'year':
            $this->errors[$part] = t('The year is missing.');
            break;
        }
      }
    }
  }
  return $true;
}