You are here

protected function DateObject::forceValid in Date 7

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

Convert a date part into something that will produce a valid date.

2 calls to DateObject::forceValid()
DateObject::arrayErrors in date_api/date_api.module
DateObject::setFuzzyDate in date_api/date_api.module
Force an incomplete date to be valid, for instance to add a valid year, month, and day if only the time has been defined.

File

date_api/date_api.module, line 555
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 forceValid($part, $value, $default = 'first', $month = NULL, $year = NULL) {
  $now = date_now();
  switch ($part) {
    case 'year':
      $fallback = $now
        ->format('Y');
      return !is_int($value) || empty($value) || $value < variable_get('date_min_year', 1) || $value > variable_get('date_max_year', 4000) ? $fallback : $value;
      break;
    case 'month':
      $fallback = $default == 'first' ? 1 : $now
        ->format('n');
      return !is_int($value) || empty($value) || $value <= 0 || $value > 12 ? $fallback : $value;
      break;
    case 'day':
      $fallback = $default == 'first' ? 1 : $now
        ->format('j');
      $max_day = isset($year) && isset($month) ? date_days_in_month($year, $month) : 31;
      return !is_int($value) || empty($value) || $value <= 0 || $value > $max_day ? $fallback : $value;
      break;
    case 'hour':
      $fallback = $default == 'first' ? 0 : $now
        ->format('G');
      return !is_int($value) || $value < 0 || $value > 23 ? $fallback : $value;
      break;
    case 'minute':
      $fallback = $default == 'first' ? 0 : $now
        ->format('i');
      return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;
      break;
    case 'second':
      $fallback = $default == 'first' ? 0 : $now
        ->format('s');
      return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;
      break;
  }
}