CronExpression_DayOfMonthField::getNearestWeekday()
Get the nearest day of the week for a given day in a month
Method of the class: CronExpression_DayOfMonthField{}
No Hooks.
Return
DateTime
. Returns the nearest date
Usage
$result = CronExpression_DayOfMonthField::getNearestWeekday( $currentYear, $currentMonth, $targetDay );
- $currentYear(int) (required)
- Current year
- $currentMonth(int) (required)
- Current month
- $targetDay(int) (required)
- Target day of the month
CronExpression_DayOfMonthField::getNearestWeekday() CronExpression DayOfMonthField::getNearestWeekday code WC 9.7.1
private static function getNearestWeekday($currentYear, $currentMonth, $targetDay) { $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT); $target = new DateTime("$currentYear-$currentMonth-$tday"); $currentWeekday = (int) $target->format('N'); if ($currentWeekday < 6) { return $target; } $lastDayOfMonth = $target->format('t'); foreach (array(-1, 1, -2, 2) as $i) { $adjusted = $targetDay + $i; if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) { $target->setDate($currentYear, $currentMonth, $adjusted); if ($target->format('N') < 6 && $target->format('m') == $currentMonth) { return $target; } } } }