function DateHelper::days

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Datetime/DateHelper.php \Drupal\Core\Datetime\DateHelper::days()
  2. 8.9.x core/lib/Drupal/Core/Datetime/DateHelper.php \Drupal\Core\Datetime\DateHelper::days()
  3. 10 core/lib/Drupal/Core/Datetime/DateHelper.php \Drupal\Core\Datetime\DateHelper::days()

Constructs an array of days in a month.

Parameters

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

int $month: (optional) The month in which to find the number of days. Defaults to NULL.

int $year: (optional) The year in which to find the number of days. Defaults to NULL.

Return value

array An array of days for the selected month.

File

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

Class

DateHelper
Defines Gregorian Calendar date values.

Namespace

Drupal\Core\Datetime

Code

public static function days($required = FALSE, $month = NULL, $year = NULL) {
    // If we have a month and year, find the right last day of the month.
    if (!empty($month) && !empty($year)) {
        $date = new DrupalDateTime($year . '-' . $month . '-01 00:00:00', 'UTC');
        $max = $date->format('t');
    }
    // If there is no month and year given, default to 31.
    if (empty($max)) {
        $max = 31;
    }
    $none = [
        '' => '',
    ];
    $range = range(1, $max);
    $range = array_combine($range, $range);
    return !$required ? $none + $range : $range;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.