1 date.class.inc protected BackdropDateTime::forceValid($part, $value, $default = 'first', $month = NULL, $year = NULL)

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

Parameters

string $part: The date part.

int $value: The date value for this part.

string $default: (optional) If the fallback should use the first value of the date part, or the current value of the date part. Defaults to 'first'.

int $month: (optional) Used when the date part is less than 'month' to specify the date. Defaults to NULL.

int $year: (optional) Used when the date part is less than 'year' to specify the date. Defaults to NULL.

Return value

int: A valid date value.

File

core/includes/date.class.inc, line 750

Class

BackdropDateTime
Extends PHP DateTime class for use with Backdrop.

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 < DATE_MIN_YEAR || $value > DATE_MAX_YEAR ? $fallback : $value;

    case 'month':
      $fallback = $default == 'first' ? 1 : $now->format('n');
      return !is_int($value) || empty($value) || $value <= 0 || $value > 12 ? $fallback : $value;

    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;

    case 'hour':
      $fallback = $default == 'first' ? 0 : $now->format('G');
      return !is_int($value) || $value < 0 || $value > 23 ? $fallback : $value;

    case 'minute':
      $fallback = $default == 'first' ? 0 : $now->format('i');
      return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;

    case 'second':
      $fallback = $default == 'first' ? 0 : $now->format('s');
      return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;
  }
  return (int) $value;
}