/** * Gets a given occurence on a given day-of-week. * * @param int $curdate The current timestamp of an occurence. * @param int $day_of_week The index of a given day-of-week. * @param int $week_of_month The index of a given week-of-month. * * @return int The timestamp of the requested occurrence. */ private function getNthDayOfWeek($curdate, $day_of_week, $week_of_month) { if ($week_of_month == -1) { // LAST WEEK $nextdate = Tribe__Date_Utils::get_last_day_of_week_in_month($curdate, $day_of_week); // If the date returned above is the same as the date we're starting from // move on to the next month by interval to consider. if ($curdate == $nextdate) { $curdate = mktime(0, 0, 0, date('n', $curdate) + $this->months_between, 1, date('Y', $curdate)); $nextdate = Tribe__Date_Utils::get_last_day_of_week_in_month($curdate, $day_of_week); } return $nextdate; } else { // get the first occurrence of the requested day of the week from the requested $curdate's month $first_occurring_day_of_week = Tribe__Date_Utils::get_first_day_of_week_in_month($curdate, $day_of_week); // get that day of the week in the requested nth week $maybe_date = strtotime(date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_FORMAT, $first_occurring_day_of_week) . ' + ' . ($week_of_month - 1) . ' weeks'); // if $maybe_date equals or is before the $curdate, then try next month // (this should only be true if $week_of_month is 1) if (date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_ONLY_FORMAT, $maybe_date) <= date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_ONLY_FORMAT, $curdate)) { // get the first day of the next month according to $this->months_between $next_month = mktime(0, 0, 0, date('n', $curdate) + $this->months_between, 1, date('Y', $curdate)); // Get the first occurrence of the requested day of the week from $next_month's month $first_occurring_day_of_week = Tribe__Date_Utils::get_first_day_of_week_in_month($next_month, $day_of_week); // Get that day of the week in the requested nth week $maybe_date = strtotime(date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_FORMAT, $first_occurring_day_of_week) . ' + ' . ($week_of_month - 1) . ' weeks'); } // if $maybe_date doesn't have the same month as $first_occurring_day_of_week, keep incrementing by $this->months_between // until they do, but don't infinitely loop past the 'recurrenceMaxMonthsAfter' setting $i = 0; while (date('n', $maybe_date) != date('n', $first_occurring_day_of_week) && $i <= tribe_get_option('recurrenceMaxMonthsAfter', 24)) { $next_month = mktime(0, 0, 0, date('n', $first_occurring_day_of_week) + $this->months_between, 1, date('Y', $first_occurring_day_of_week)); $first_occurring_day_of_week = Tribe__Date_Utils::get_first_day_of_week_in_month($next_month, $day_of_week); $maybe_date = strtotime(date(Tribe__Events__Pro__Date_Series_Rules__Rules_Interface::DATE_FORMAT, $first_occurring_day_of_week) . ' + ' . ($week_of_month - 1) . ' weeks'); $i += $this->months_between; } return $maybe_date; } }