private function previous(LocalDate $date) { $newDayOfMonth = $date->getDayOfMonth() - 1; if ($newDayOfMonth > 0) { return $date->withDayOfMonth($newDayOfMonth); } $date = $date->adjust($date->getMonth()->minus(1)); if ($date->getMonth() == Month::DECEMBER()) { $date = $date->withYear($date->getYear() - 1); } return $date->withDayOfMonth($date->getMonth()->length(Year::isLeapYear($date->getYear()))); }
/** * Returns a copy of this {@code LocalDateTime} with the day-of-month altered. * <p> * If the resulting date-time is invalid, an exception is thrown. * The time does not affect the calculation and will be the same in the result. * <p> * This instance is immutable and unaffected by this method call. * * @param int $dayOfMonth the day-of-month to set in the result, from 1 to 28-31 * @return LocalDateTime a {@code LocalDateTime} based on this date-time with the requested day, not null * @throws DateTimeException if the day-of-month value is invalid, * or if the day-of-month is invalid for the month-year */ public function withDayOfMonth($dayOfMonth) { return $this->_with($this->date->withDayOfMonth($dayOfMonth), $this->time); }