Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DateHelper.php

function DateHelper::years

Constructs an array of years in a specified range.

Parameters

int $min: (optional) The minimum year in the array. Defaults to zero.

int $max: (optional) The maximum year in the array. Defaults to zero.

bool $required: (optional) If FALSE, the returned array will include a blank value. Defaults to FALSE.

Return value

array An array of years in the selected range.

File

core/lib/Drupal/Core/Datetime/DateHelper.php, line 291

Class

DateHelper
Defines Gregorian Calendar date values.

Namespace

Drupal\Core\Datetime

Code

public static function years($min = 0, $max = 0, $required = FALSE) {
    // Ensure $min and $max are valid values.
    $requestTime = \Drupal::time()->getRequestTime();
    if (empty($min)) {
        $min = intval(date('Y', $requestTime) - 3);
    }
    if (empty($max)) {
        $max = intval(date('Y', $requestTime) + 3);
    }
    $none = [
        '' => '',
    ];
    $range = range($min, $max);
    $range = array_combine($range, $range);
    return !$required ? $none + $range : $range;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal