/**
  * Test that date can determine if it is in a leap year
  */
 public function testIsLeapyear()
 {
     $date = new qCal_Date(2009, 4, 23);
     $this->assertFalse($date->isLeapYear());
     $date2 = new qCal_Date(2008, 4, 23);
     $this->assertTrue($date2->isLeapYear());
 }
Example #2
0
 /**
  * Determine the number or Tuesdays (or whatever day of the week this date is) since the
  * beginning or end of the year.
  */
 public function getXthWeekdayOfYear($xth, $weekday = 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($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 find the specified day by counting either forwards or backwards to the day in question
     $firstofyear = new qCal_Date($year, 1, 1);
     $numdaysinyear = $firstofyear->isLeapYear() ? 366 : 365;
     $numweekdays = 0;
     // the number of weekdays that have occurred within the loop
     $found = false;
     // whether or not the specified day has been found
     if ($negpos == "+") {
         // count forward
         // loop over every day of every month looking for the right one
         $day = 1;
         $wday = $firstofyear->getWeekDay();
         while ($day <= $numdaysinyear) {
             // 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!
                     $found = $day;
                     break;
                 }
             }
             if ($wday == 6) {
                 $wday = 0;
             } else {
                 $wday++;
             }
             $day++;
         }
     } else {
         // count backward
         $lastofyear = new qCal_Date($year, 12, 31);
         // count forward
         // loop over every day of every month looking for the right one
         $day = $numdaysinyear;
         $wday = $lastofyear->getWeekDay();
         while ($day >= 1) {
             // 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!
                     $found = $day;
                     break;
                 }
             }
             if ($wday == 0) {
                 $wday = 6;
             } else {
                 $wday--;
             }
             $day--;
         }
     }
     // @todo: Can't use checkdate here, so find another validation method...
     if ($found) {
         $date = new qCal_Date($year, 1, $found, true);
         // takes advantage of the rollover feature :)
     } else {
         throw new qCal_DateTime_Exception_InvalidDate("You have specified an incorrect number of days for qCal_Date::getXthWeekdayOfYear()");
     }
     return $date;
 }