
PHP의 DateTimeImmutable 클래스는 timezone을 설정하는 방법을 제공합니다. timezone을 설정하는 방법은 다음과 같습니다.
#hostingforum.kr
php
$date = new DateTimeImmutable('2022-01-01 12:00:00', new DateTimeZone('Asia/Seoul'));
위 코드는 2022년 1월 1일 12:00:00에 Asia/Seoul timezone을 설정합니다.
timezone 설정이 날짜와 시간에 영향을 미치는지 궁금하셨다면, timezone이 설정되지 않은 경우와 timezone이 설정된 경우를 비교해 보면 다음과 같습니다.
#hostingforum.kr
php
// timezone이 설정되지 않은 경우
$date1 = new DateTimeImmutable('2022-01-01 12:00:00');
echo $date1->format('Y-m-d H:i:sP') . "n"; // 2022-01-01 12:00:00+09:00
// timezone이 설정된 경우
$date2 = new DateTimeImmutable('2022-01-01 12:00:00', new DateTimeZone('Asia/Seoul'));
echo $date2->format('Y-m-d H:i:sP') . "n"; // 2022-01-01 12:00:00+09:00
위 코드에서 보듯이 timezone이 설정되지 않은 경우와 timezone이 설정된 경우의 결과는 같습니다. 이는 DateTimeImmutable 클래스가 timezone을 자동으로 설정하기 때문입니다.
하지만, timezone이 설정되지 않은 경우에는 timezone이 자동으로 설정되지 않을 수 있습니다. 이 경우 timezone을 수동으로 설정해야 합니다.
#hostingforum.kr
php
// timezone이 설정되지 않은 경우
$date1 = new DateTimeImmutable('2022-01-01 12:00:00');
$date1->setTimezone(new DateTimeZone('Asia/Seoul'));
echo $date1->format('Y-m-d H:i:sP') . "n"; // 2022-01-01 12:00:00+09:00
위 코드에서 보듯이 timezone이 설정되지 않은 경우 timezone을 수동으로 설정하여 사용할 수 있습니다.
2025-07-24 16:04