public function testDateGetXthWeekdayOfMonthExamples() { $date = new qCal_Date(2010, 1, 10); // Sunday, January 10th, 2010 $first_sunday_in_january = $date->getXthWeekdayOfMonth(1); // will return a qCal_Date object for January 3rd, 2010 $this->assertEqual($first_sunday_in_january->__toString(), "01/03/2010"); $first_tuesday_in_january = $date->getXthWeekdayOfMonth(1, "tuesday"); // will return a qCal_Date object for January 5th, 2010 $this->assertEqual($first_tuesday_in_january->__toString(), "01/05/2010"); $second_to_last_wednesday_in_january = $date->getXthWeekdayOfMonth(-2, "wednesday"); // will return a qCal_Date object for January 20th, 2010 $this->assertEqual($second_to_last_wednesday_in_january->__toString(), "01/20/2010"); $second_monday_in_february = $date->getXthWeekdayOfMonth(2, 1, 2); // will return qCal_Date object for February 8th, 2010 $this->assertEqual($second_monday_in_february->__toString(), "02/08/2010"); $second_monday_in_february = $date->getXthWeekdayOfMonth(2, "monday", "february"); // the previous example could also be done like this $this->assertEqual($second_monday_in_february->__toString(), "02/08/2010"); $last_sunday_in_august = $date->getXthWeekdayOfMonth(-1, "sunday", "august"); // will return qCal_Date object for August 29th, 2010 $this->assertEqual($last_sunday_in_august->__toString(), "08/29/2010"); $last_sunday_in_december_2011 = $date->getXthWeekdayOfMonth(-1, "sunday", "december", 2011); // will return qCal_Date object for December 25th, 2011 $this->assertEqual($last_sunday_in_december_2011->__toString(), "12/25/2011"); $first_sunday_in_january_2012 = $date->getXthWeekdayOfMonth(1, 0, 1, 2012); // will return qCal_Date object for January 1st, 2012 $this->assertEqual($first_sunday_in_january_2012->__toString(), "01/01/2012"); }
/** * Check a qCal_Date object to see if it falls within the rules specified * when this object was created (or added later actually). * @param qCal_Date The date that you want to check * @return boolean True if the date falls within the ruleset * @access public */ public function checkDate(qCal_Date $date) { $byDay = $this->getValues(); foreach ($byDay as $wday) { $char1 = substr($wday, 0, 1); $cwday = strtoupper(substr($date->getWeekDayName(), 0, 2)); if (ctype_digit($char1) || $char1 == "-" || $char1 == "+") { // if the first character is a digit or a plus or minus, we // need to check that date is a specific weekday of the month if (preg_match('/([+-]?)([0-9]+)([a-z]+)/i', $wday, $matches)) { list($whole, $sign, $dig, $wd) = $matches; // find out if this day matches the specific weekday of month $xth = (int) ($sign . $dig); // @todo Make sure that getXthWeekDayOfMonth doesn't need // to be passed any month or date here... $dtWdSpecific = $date->getXthWeekdayOfMonth($xth, $wd); if ($dtWdSpecific->__toString() == $date->__toString()) { return true; } } } else { if ($wday == $cwday) { return true; } } } return false; }
/** * Test that an exception is thrown if there is a request for an non-existant weekday in the month */ public function testInvalidXthWeekday() { $this->expectException(new qCal_DateTime_Exception_InvalidDate("You have specified an incorrect number of days for qCal_Date::getXthWeekdayOfMonth()")); $date = new qCal_Date(2010, 1, 1); $tenth_tuesday = $date->getXthWeekdayOfMonth(10, "Tuesday"); }