Пример #1
0
 /**
  * @param integer      $dayOfMonth  The day-of-month to check, validated as an integer.
  * @param integer|null $monthOfYear An optional month-of-year to check against, fully validated.
  * @param integer|null $year        An optional year to check against, fully validated.
  *
  * @return void
  *
  * @throws DateTimeException If the day-of-month is not valid.
  */
 public static function check($dayOfMonth, $monthOfYear = null, $year = null)
 {
     if ($dayOfMonth < 1 || $dayOfMonth > 31) {
         throw DateTimeException::fieldNotInRange(self::NAME, $dayOfMonth, 1, 31);
     }
     if ($monthOfYear === null) {
         return;
     }
     $monthLength = MonthOfYear::getLength($monthOfYear, $year);
     if ($dayOfMonth > $monthLength) {
         if ($dayOfMonth === 29) {
             throw new DateTimeException("Invalid date February 29 as {$year} is not a leap year");
         } else {
             $monthName = MonthOfYear::getName($monthOfYear);
             throw new DateTimeException("Invalid date {$monthName} {$dayOfMonth}");
         }
     }
 }
Пример #2
0
 /**
  * Returns the length of the month represented by this date.
  *
  * @return integer The length of the month in days.
  */
 public function getLengthOfMonth()
 {
     return Field\MonthOfYear::getLength($this->month, $this->year);
 }
Пример #3
0
 /**
  * Returns a copy of this MonthDay with the month-of-year altered.
  *
  * If the day-of-month is invalid for the specified month, the day will
  * be adjusted to the last valid day-of-month.
  *
  * @param integer $month
  *
  * @return MonthDay
  *
  * @throws DateTimeException If the month is invalid.
  */
 public function withMonth($month)
 {
     $month = Cast::toInteger($month);
     if ($month === $this->month) {
         return $this;
     }
     Field\MonthOfYear::check($month);
     $lastDay = Field\MonthOfYear::getLength($month);
     return new MonthDay($month, $lastDay < $this->day ? $lastDay : $this->day);
 }