You are here

function date_calc_n_weekday_of_month in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_php4/date_php4_calc.inc \date_calc_n_weekday_of_month()
  2. 6 date_php4/date_php4_calc.inc \date_calc_n_weekday_of_month()

Calculates the date of the Nth weekday of the month, such as the second Saturday of January 2000

Parameters

int $week: The number of the week to get (1 = first, etc. Also can be 'last'.)

int $dow: The day of the week (0 = Sunday).

int $month: The month.

int $year: The 4 digit year. Do not add leading 0's for years prior to 1000.

string $format: The string indicating how to format the output.

Return value

string The date in the desired format.

File

date_php4/date_php4_calc.inc, line 1348

Code

function date_calc_n_weekday_of_month($week, $dow, $month, $year, $format = DATE_CALC_FORMAT) {
  if (is_numeric($week)) {
    $DOW1day = ($week - 1) * 7 + 1;
    $DOW1 = date_dow($DOW1day, $month, $year);
    $wdate = ($week - 1) * 7 + 1 + (7 + $dow - $DOW1) % 7;
    if ($wdate > date_calc_days_in_month($month, $year)) {
      return -1;
    }
    else {
      return date_calc_format($wdate, $month, $year, $format);
    }
  }
  elseif ($week == 'last' && $dow < 7) {
    $lastday = date_calc_days_in_month($month, $year);
    $lastdow = date_dow($lastday, $month, $year);
    $diff = $dow - $lastdow;
    if ($diff > 0) {
      return date_calc_format($lastday - (7 - $diff), $month, $year, $format);
    }
    else {
      return date_calc_format($lastday + $diff, $month, $year, $format);
    }
  }
  else {
    return -1;
  }
}