WordPress at Your Fingertips
function is not described

CronExpression_DayOfMonthField::getNearestWeekday() private WC 1.0

Get the nearest day of the week for a given day in a month

{} It's a 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

Code of CronExpression_DayOfMonthField::getNearestWeekday() WC 6.1.0

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;
            }
        }
    }
}