Horde_Date::setNthWeekday PHP Method

setNthWeekday() public method

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 setNthWeekday($weekday, $nth = 1)
    {
        if ($weekday < self::DATE_SUNDAY || $weekday > self::DATE_SATURDAY) {
            return;
        }
        if ($nth >= 0) {
            $this->_mday = 1;
            $first = $this->dayOfWeek();
            $this->_mday = $weekday - $first + 1;
            if ($weekday < $first) {
                $this->_mday += 7;
            }
            $diff = 7 * $nth - 7;
            $this->_mday += $diff;
            $this->_correct(self::MASK_DAY, false);
        } else {
            $this->_mday = Horde_Date_Utils::daysInMonth($this->_month, $this->_year);
            $last = $this->dayOfWeek();
            $this->_mday -= $last - $weekday;
            if ($last < $weekday) {
                $this->_mday -= 7;
            }
            $diff = -7 * $nth - 7;
            $this->_mday -= $diff;
            $this->_correct(self::MASK_DAY, true);
            $this->_formatCache = array();
        }
    }

Usage Example

Exemplo n.º 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);
 }
All Usage Examples Of Horde_Date::setNthWeekday