private static function getNearestWeekday($currentYear, $currentMonth, $targetDay)
    {
        $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT);
        $target = DateTime::createFromFormat('Y-m-d', "$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;
                }
            }
        }
    }
private static function getNearestWeekday($currentYear, $currentMonth, $targetDay)
    {
        $tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT);
        $target = DateTime::createFromFormat('Y-m-d', "$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;
                }
            }
        }
    }
private static function getNearestWeekday ( $ currentYear , $ currentMonth , $ targetDay ) { $ tday = str_pad ( $ targetDay , 2 , '0' , STR_PAD_LEFT ) ; $ target = DateTime :: createFromFormat ( 'Y-m-d' , "$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 ; } } } }
Get the nearest day of the week for a given day in a month

@param int $currentYear  Current year
@param int $currentMonth Current month
@param int $targetDay    Target day of the month

@return \DateTime Returns the nearest date
Get the nearest day of the week for a given day in a month