/** * 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; }
/** * Which weekday of the month or year it is (ie: 2nd Tuesday in January, last Sunday of * the month, fourth from last Monday of the year, etc.) */ public function testDateComponentCanDetermineWhichWeekDayOfMonthOrYearItIs() { $date = new qCal_Date(2009, 2, 22); $this->assertEqual($date->getWeekDayName(), "Sunday"); $this->assertEqual($date->getXthWeekdayOfMonth(-1), $date); // last sunday of the month $this->assertEqual($date->getXthWeekdayOfMonth(4), $date); // fourth sunday of the month $this->assertNotEqual($date->getXthWeekdayOfMonth(2), $date); // NOT the second Sunday of the month $this->assertEqual($date->getXthWeekdayOfYear(8), $date); // eighth sunday of the year $this->assertEqual($date->getXthWeekdayOfYear(-45), $date); }
/** * The date object has many getters which allow for you to determine things like day of the week, * day of the year, etc. The following tests those getters. */ public function testGetters() { $date = new qCal_Date(2009, 4, 23); /** * Month */ $this->assertEqual($date->getMonth(), 4); $this->assertEqual($date->getMonthName(), "April"); $this->assertEqual($date->getNumDaysInMonth(), 30); /** * Day */ $this->assertEqual($date->getDay(), 23); $this->assertEqual($date->getYearDay(), 112); $this->assertEqual($date->getFirstDayOfMonth()->__toString(), "04/01/2009"); $this->assertEqual($date->getFirstDayOfMonth()->format("l"), "Wednesday"); $this->assertEqual($date->getLastDayOfMonth()->__toString(), "04/30/2009"); $this->assertEqual($date->getLastDayOfMonth()->format("l"), "Thursday"); // find the xth weekday (mon-sun) of the month $this->assertEqual($date->getXthWeekdayOfMonth(2)->__toString(), "04/09/2009"); // find the second Thursday of the month (Because 4/23/2009 was on a Thursday, the weekday defaults to that. The year defaults to 2009 for basically the same reason) $this->assertEqual($date->getXthWeekdayOfMonth(2, "Monday")->__toString(), "04/13/2009"); // find the second monday of the month (month defaults to april because that's what $date is currently set to) $this->assertEqual($date->getXthWeekdayOfMonth(2, "Monday", "January")->__toString(), "01/12/2009"); // find the second monday in January (year defaults to 2009) $this->assertEqual($date->getXthWeekdayOfMonth(2, "Monday", "January", 2008)->__toString(), "01/14/2008"); // find the second Monday in January, 2008 // now try negatives and positives $this->assertEqual($date->getXthWeekdayOfMonth(-2)->__toString(), "04/23/2009"); // get the second to last Thursday of the month $this->assertEqual($date->getXthWeekdayOfMonth("-2")->__toString(), "04/23/2009"); // get the second to last Thursday of the month $this->assertEqual($date->getXthWeekdayOfMonth(+2)->__toString(), "04/09/2009"); // surprisingly, this works... interesting... $this->assertEqual($date->getXthWeekdayOfMonth("+2")->__toString(), "04/09/2009"); // we can also use numbers instead of spelling out the names of weekdays and months. For the weekday part, use 0 for Sunday through 6 for Saturday (the same as PHP's date function's "w" metacharacter) $this->assertEqual($date->getXthWeekdayOfMonth(2, 1)->__toString(), "04/13/2009"); // second monday $this->assertEqual($date->getXthWeekdayOfMonth(2, 1, 1)->__toString(), "01/12/2009"); // second monday in january $this->assertEqual($date->getXthWeekdayOfMonth(-2, 1)->__toString(), "04/20/2009"); // second to last monday in april /** * Year */ $this->assertEqual($date->getYear(), 2009); /** * Week */ $this->assertEqual($date->getWeekDay(), 4); $this->assertEqual($date->getWeekDayName(), "Thursday"); $this->assertEqual($date->getWeekOfYear(), 17); /** * Unix Timestamp */ $this->assertEqual($date->getUnixTimestamp(), gmmktime(0, 0, 0, 4, 23, 2009)); }