/** * Calculates the date of the Nth weekday of the month, * such as the second Saturday of January 2000 * * @param int $week * the number of the week to get * (1 = first, etc. Also can be 'last'.) * @param int $dow * the day of the week (0 = Sunday) * @param int $month * the month * @param int $year * the year. Use the complete year instead of the * abbreviated version. E.g. use 2005, not 05. * Do not add leading 0's for years prior to 1000. * @param string $format * the string indicating how to format the output * * @return string the date in the desired format * * @access public * @static * */ static function NWeekdayOfMonth($week, $dow, $month, $year, $format = DATE_CALC_FORMAT) { if (is_numeric($week)) { $DOW1day = ($week - 1) * 7 + 1; $DOW1 = Calc::dayOfWeek($DOW1day, $month, $year); $wdate = ($week - 1) * 7 + 1 + (7 + $dow - $DOW1) % 7; if ($wdate > Calc::daysInMonth($month, $year)) { return -1; } else { return Calc::dateFormat($wdate, $month, $year, $format); } } elseif ($week == 'last' && $dow < 7) { $lastday = Calc::daysInMonth($month, $year); $lastdow = Calc::dayOfWeek($lastday, $month, $year); $diff = $dow - $lastdow; if ($diff > 0) { return Calc::dateFormat($lastday - (7 - $diff), $month, $year, $format); } else { return Calc::dateFormat($lastday + $diff, $month, $year, $format); } } else { return -1; } }