You are here

public static function DateRecurModularUtilityTrait::getMonthWeekdayNth in Recurring Date Field Modular Widgets 3.x

Same name and namespace in other branches
  1. 8 src/DateRecurModularUtilityTrait.php \Drupal\date_recur_modular\DateRecurModularUtilityTrait::getMonthWeekdayNth()
  2. 2.x src/DateRecurModularUtilityTrait.php \Drupal\date_recur_modular\DateRecurModularUtilityTrait::getMonthWeekdayNth()

Determine nth weekday into a month for a date.

Given the weekday and month for a date, attempt to determine how many weekdays into the month.

Parameters

\DateTime $date: A date.

Return value

int A number between 1 and 5.

3 calls to DateRecurModularUtilityTrait::getMonthWeekdayNth()
DateRecurModularSierraModalForm::buildForm in src/Form/DateRecurModularSierraModalForm.php
Form constructor.
DateRecurModularSierraWidget::buildRruleFromRecurrenceOption in src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php
Builds a RRULE string from a preset option given a particular start date.
DateRecurModularSierraWidget::getRecurrenceOptions in src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php
Get recurrence options for a select element based on a start date.

File

src/DateRecurModularUtilityTrait.php, line 247

Class

DateRecurModularUtilityTrait
Trait containing convenience methods for dealing with date recur widgets.

Namespace

Drupal\date_recur_modular

Code

public static function getMonthWeekdayNth(\DateTime $date) : int {
  $monthWeekdayNth = 0;
  $weekdayNumber = $date
    ->format('w');
  $interval = new \DateInterval('P1D');
  $iter = clone $date;
  $iter
    ->setDate((int) $iter
    ->format('Y'), (int) $iter
    ->format('m'), 1);
  while ($iter <= $date) {
    if ($iter
      ->format('w') === $weekdayNumber) {
      $monthWeekdayNth++;
    }
    $iter
      ->add($interval);
  }
  return $monthWeekdayNth;
}