/** * @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}"); } } }
/** * 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); }
/** * 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); }
/** * Returns a copy of this YearMonth with the month-of-year altered. * * @param integer $month * * @return YearMonth */ public function withMonth($month) { $month = Cast::toInteger($month); if ($month === $this->month) { return $this; } Field\MonthOfYear::check($month); return new YearMonth($this->year, $month); }
/** * Returns an instance of Month for the given month value. * * @param integer $value The month number, from 1 (January) to 12 (December). * * @return Month The Month instance. * * @throws DateTimeException */ public static function of($value) { $value = Cast::toInteger($value); Field\MonthOfYear::check($value); return Month::get($value); }