
DateTimeImmutable::getErrors() 메서드는 DateTimeImmutable 객체의 생성 시점에 발생한 오류를 반환합니다. 이 메서드는 DateTimeImmutable::getLastErrors() 메서드와는 다르게, DateTimeImmutable::getLastErrors() 메서드는 DateTimeImmutable 객체가 생성된 이후에 발생한 오류를 반환합니다.
예를 들어, 다음과 같은 코드를 실행한 경우:
#hostingforum.kr
php
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2022-13-01');
echo $date->getErrors()[0];
출력 결과는 다음과 같습니다:
#hostingforum.kr
DateTimeImmutable::__construct(): Failed to parse time string (2022-13-01) at position 5 (1): Unexpected character
반면에, DateTimeImmutable::getLastErrors() 메서드는 다음과 같은 코드를 실행한 경우:
#hostingforum.kr
php
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2022-13-01');
$date->modify('+1 day');
echo $date->getLastErrors()[0];
출력 결과는 다음과 같습니다:
#hostingforum.kr
DateTimeImmutable::__construct(): Failed to parse time string (2022-13-01) at position 5 (1): Unexpected character
위 예제에서, DateTimeImmutable::getErrors() 메서드는 DateTimeImmutable 객체가 생성된 시점에 발생한 오류를 반환합니다. 반면에, DateTimeImmutable::getLastErrors() 메서드는 DateTimeImmutable 객체가 생성된 이후에 발생한 오류를 반환합니다.
2025-04-07 03:08