function DateHelper::weekDaysOrdered

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

Reorders weekdays to match the first day of the week.

Parameters

array $weekdays: An array of weekdays.

Return value

array An array of weekdays reordered to match the first day of the week. The keys will remain unchanged. For example, if the first day of the week is set to be Monday, the array keys will be [1, 2, 3, 4, 5, 6, 0].

1 call to DateHelper::weekDaysOrdered()
DateHelperTest::testWeekDaysOrdered in core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php
@covers ::weekDaysOrdered @dataProvider providerTestWeekDaysOrdered

File

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

Class

DateHelper
Defines Gregorian Calendar date values.

Namespace

Drupal\Core\Datetime

Code

public static function weekDaysOrdered($weekdays) {
    $first_day = \Drupal::config('system.date')->get('first_day');
    if ($first_day > 0) {
        for ($i = 1; $i <= $first_day; $i++) {
            // Reset the array to the first element.
            reset($weekdays);
            // Retrieve the first week day value.
            $last = current($weekdays);
            // Store the corresponding key.
            $key = key($weekdays);
            // Remove this week day from the beginning of the array.
            unset($weekdays[$key]);
            // Add this week day to the end of the array.
            $weekdays[$key] = $last;
        }
    }
    return $weekdays;
}

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