
DateTimeInterface::getTimestamp()와 DateTimeImmutable::getTimestamp()의 차이점은 Mutable과 Immutable의 차이점에 있습니다.
DateTimeInterface는 Mutable한 객체입니다. getTimestamp() 메서드를 호출하면 timestamp가 변할 수 있습니다. 예를 들어, 다음과 같이 timestamp를 변경할 수 있습니다.
#hostingforum.kr
php
$datetimeInterface = new DateTime('2022-01-01 12:00:00');
$timestamp = $datetimeInterface->getTimestamp();
$datetimeInterface->setTimestamp($timestamp + 3600); // 1시간 추가
DateTimeImmutable은 Immutable한 객체입니다. getTimestamp() 메서드를 호출하면 timestamp가 변하지 않습니다. 예를 들어, 다음과 같이 timestamp를 변경하려고 하면 에러가 발생합니다.
#hostingforum.kr
php
$datetimeImmutable = new DateTimeImmutable('2022-01-01 12:00:00');
$timestamp = $datetimeImmutable->getTimestamp();
$datetimeImmutable->setTimestamp($timestamp + 3600); // 에러 발생
따라서, DateTimeImmutable을 사용할 때는 timestamp를 변경할 수 없으므로, timestamp를 변경해야 하는 경우에는 DateTimeInterface를 사용하는 것이 좋습니다.
2025-03-25 22:10