
DatePeriod::getEndDate를 사용할 때 start date가 null인 경우, PHP의 DateTime 클래스에서 getEndDate() 메서드는 null을 반환합니다.
따라서 start date가 null인 경우, getEndDate() 메서드를 사용하기 전에 start date가 null인지 확인하는 코드를 추가하는 것이 좋습니다.
예를 들어, 다음과 같이 코드를 작성할 수 있습니다.
#hostingforum.kr
php
$start_date = new DateTime('2022-01-01');
$end_date = new DatePeriod($start_date, new DateInterval('P1D'), new DateInterval('P10D'))->getEndDate();
if ($start_date === null) {
$end_date = null;
} else {
$end_date = $start_date->add(new DateInterval('P10D'));
}
또는, start date가 null인 경우, end date를 null로 반환하도록 수정할 수도 있습니다.
#hostingforum.kr
php
public function getEndDate($start_date)
{
if ($start_date === null) {
return null;
}
return $start_date->add(new DateInterval('P10D'));
}
이러한 방법으로 start date가 null인 경우를 처리할 수 있습니다.
2025-04-10 14:03