Ejemplo n.º 1
0
 /**
  * Determine the number or Tuesdays (or whatever day of the week this date is) since the
  * beginning or end of the month.
  * @param integer $xth A positive or negative number that determines which weekday of the month we want
  * @param string|integer $weekday Either Sunday-Saturday or 0-6 to specify the weekday we want
  * @param string|integer $month Either January-December or 1-12 to specify the month we want
  * @param integer $year A valid year to specify which year we want
  */
 public function getXthWeekdayOfMonth($xth, $weekday = null, $month = null, $year = null)
 {
     $negpos = substr($xth, 0, 1);
     if ($negpos == "+" || $negpos == "-") {
         $xth = (int) substr($xth, 1);
     } else {
         $negpos = "+";
     }
     if (is_null($weekday)) {
         $weekday = $this->getWeekday();
     }
     if (ctype_digit((string) $weekday)) {
         if (!array_key_exists($weekday, $this->weekdays)) {
             throw new qCal_Date_Exception_InvalidWeekday("\"{$weekday}\" is not a valid weekday.");
         }
     } else {
         $weekday = strtolower($weekday);
         if (!in_array($weekday, $this->weekdays)) {
             throw new qCal_Date_Exception_InvalidWeekday("\"{$weekday}\" is not a valid weekday.");
         }
         $wdays = array_flip($this->weekdays);
         $weekday = $wdays[$weekday];
     }
     if (is_null($month)) {
         $month = $this->getMonth();
     }
     if (ctype_digit((string) $month)) {
         if (!array_key_exists($month, $this->months)) {
             throw new qCal_Date_Exception_InvalidMonth("\"{$month}\" is not a valid month.");
         }
     } else {
         $month = strtolower($month);
         if (!in_array($month, $this->months)) {
             throw new qCal_Date_Exception_InvalidMonth("\"{$month}\" is not a valid month.");
         }
         $mons = array_flip($this->months);
         $month = $mons[$month];
     }
     if (is_null($year)) {
         $year = $this->getYear();
     }
     if (!ctype_digit((string) $year) || strlen($year) != 4) {
         throw new qCal_Date_Exception_InvalidYear("\"{$year}\" is not a valid year.");
     }
     // now, using the year, month and numbered weekday, we need to find the actual day of the month...
     $firstofmonth = new qCal_Date($year, $month, 1);
     $numdaysinmonth = $firstofmonth->getNumDaysInMonth();
     $numweekdays = 0;
     // the number of weekdays that have occurred (in the loop)
     $foundday = false;
     if ($negpos == "+") {
         $day = 1;
         $wday = $firstofmonth->getWeekday();
         // while we are in the current month, loop
         while ($day <= $numdaysinmonth) {
             // if the specified weekday == the current week day in the loop
             if ($weekday == $wday) {
                 $numweekdays++;
                 if ($numweekdays == $xth) {
                     // break out of the loop, we've found the right day! yay!
                     $foundday = $day;
                     break;
                 }
             }
             if ($wday == 6) {
                 $wday = 0;
             } else {
                 $wday++;
             }
             $day++;
         }
     } else {
         $day = $numdaysinmonth;
         $lastofmonth = $firstofmonth->getLastDayOfMonth();
         $wday = $lastofmonth->getWeekday();
         while ($day >= 1) {
             if ($weekday == $wday) {
                 $numweekdays++;
                 if ($numweekdays == $xth) {
                     // break out of the loop, we've found the right day! yay!
                     $foundday = $day;
                     break;
                 }
             }
             if ($wday == 0) {
                 $wday = 6;
             } else {
                 $wday--;
             }
             $day--;
         }
     }
     if ($foundday && checkdate($month, $day, $year)) {
         $date = new qCal_Date($year, $month, $day);
     } else {
         if ($day == 32) {
             throw new qCal_DateTime_Exception_InvalidDate("You have specified an incorrect number of days for qCal_Date::getXthWeekdayOfMonth()");
         } else {
             throw new qCal_DateTime_Exception_InvalidDate("You have entered an invalid date.");
         }
     }
     return $date;
 }