Sets the date of this object to the $nth weekday of $weekday.
public setNthWeekday ( integer $weekday, integer $nth = 1 ) | ||
$weekday | integer | The day of the week (0 = Sunday, etc). |
$nth | integer | The $nth $weekday to set to (defaults to 1). Negative values count from end of the month (@since Horde_Date 2.1.0). |
public function testSetNthWeekday() { $date = new Horde_Date('2004-10-01'); $date->setNthWeekday(Horde_Date::DATE_SATURDAY); $this->assertEquals(2, $date->mday); $date->setNthWeekday(Horde_Date::DATE_SATURDAY, 2); $this->assertEquals(9, $date->mday); $date = new Horde_Date('2007-04-01'); $date->setNthWeekday(Horde_Date::DATE_THURSDAY); $this->assertEquals(5, $date->mday); }
/** * Parses the recurrence data from a Kolab hash. * * @param array $hash The hash to convert. * * @return boolean True if the hash seemed valid, false otherwise. */ public function fromKolab($hash) { $this->reset(); if (!isset($hash['interval']) || !isset($hash['cycle'])) { $this->setRecurType(self::RECUR_NONE); return false; } $this->setRecurInterval((int) $hash['interval']); $parse_day = false; $set_daymask = false; $update_month = false; $update_daynumber = false; $update_weekday = false; $nth_weekday = -1; switch ($hash['cycle']) { case 'daily': $this->setRecurType(self::RECUR_DAILY); break; case 'weekly': $this->setRecurType(self::RECUR_WEEKLY); $parse_day = true; $set_daymask = true; break; case 'monthly': if (!isset($hash['daynumber'])) { $this->setRecurType(self::RECUR_NONE); return false; } switch ($hash['type']) { case 'daynumber': $this->setRecurType(self::RECUR_MONTHLY_DATE); $update_daynumber = true; break; case 'weekday': $this->setRecurType(self::RECUR_MONTHLY_WEEKDAY); $nth_weekday = (int) $hash['daynumber']; $hash['daynumber'] = 1; $parse_day = true; $update_daynumber = true; $update_weekday = true; break; } break; case 'yearly': if (!isset($hash['type'])) { $this->setRecurType(self::RECUR_NONE); return false; } switch ($hash['type']) { case 'monthday': $this->setRecurType(self::RECUR_YEARLY_DATE); $update_month = true; $update_daynumber = true; break; case 'yearday': if (!isset($hash['daynumber'])) { $this->setRecurType(self::RECUR_NONE); return false; } $this->setRecurType(self::RECUR_YEARLY_DAY); // Start counting days in January. $hash['month'] = 'january'; $update_month = true; $update_daynumber = true; break; case 'weekday': if (!isset($hash['daynumber'])) { $this->setRecurType(self::RECUR_NONE); return false; } $this->setRecurType(self::RECUR_YEARLY_WEEKDAY); $nth_weekday = (int) $hash['daynumber']; $hash['daynumber'] = 1; $parse_day = true; $update_month = true; $update_daynumber = true; $update_weekday = true; break; } } if (isset($hash['range-type']) && isset($hash['range'])) { switch ($hash['range-type']) { case 'number': $this->setRecurCount((int) $hash['range']); break; case 'date': $recur_end = new Horde_Date($hash['range']); $recur_end->hour = 23; $recur_end->min = 59; $recur_end->sec = 59; $this->setRecurEnd($recur_end); break; } } // Need to parse <day>? $last_found_day = -1; if ($parse_day) { if (!isset($hash['day'])) { $this->setRecurType(self::RECUR_NONE); return false; } $mask = 0; $bits = array('monday' => Horde_Date::MASK_MONDAY, 'tuesday' => Horde_Date::MASK_TUESDAY, 'wednesday' => Horde_Date::MASK_WEDNESDAY, 'thursday' => Horde_Date::MASK_THURSDAY, 'friday' => Horde_Date::MASK_FRIDAY, 'saturday' => Horde_Date::MASK_SATURDAY, 'sunday' => Horde_Date::MASK_SUNDAY); $days = array('monday' => Horde_Date::DATE_MONDAY, 'tuesday' => Horde_Date::DATE_TUESDAY, 'wednesday' => Horde_Date::DATE_WEDNESDAY, 'thursday' => Horde_Date::DATE_THURSDAY, 'friday' => Horde_Date::DATE_FRIDAY, 'saturday' => Horde_Date::DATE_SATURDAY, 'sunday' => Horde_Date::DATE_SUNDAY); foreach ($hash['day'] as $day) { // Validity check. if (empty($day) || !isset($bits[$day])) { continue; } $mask |= $bits[$day]; $last_found_day = $days[$day]; } if ($set_daymask) { $this->setRecurOnDay($mask); } } if ($update_month || $update_daynumber || $update_weekday) { if ($update_month) { $month2number = array('january' => 1, 'february' => 2, 'march' => 3, 'april' => 4, 'may' => 5, 'june' => 6, 'july' => 7, 'august' => 8, 'september' => 9, 'october' => 10, 'november' => 11, 'december' => 12); if (isset($month2number[$hash['month']])) { $this->start->month = $month2number[$hash['month']]; } } if ($update_daynumber) { if (!isset($hash['daynumber'])) { $this->setRecurType(self::RECUR_NONE); return false; } $this->start->mday = $hash['daynumber']; } if ($update_weekday) { $this->start->setNthWeekday($last_found_day, $nth_weekday); } } // Exceptions. if (isset($hash['exclusion'])) { foreach ($hash['exclusion'] as $exception) { if ($exception instanceof DateTime) { $this->exceptions[] = $exception->format('Ymd'); } } } if (isset($hash['complete'])) { foreach ($hash['complete'] as $completion) { if ($exception instanceof DateTime) { $this->completions[] = $completion->format('Ymd'); } } } return true; }