라이브러리

[PHP] IntlTimeZone::toDateTimeZone - DateTimeZone 객체로 변환




IntlTimeZone::toDateTimeZone

`IntlTimeZone::toDateTimeZone`은 PHP의 IntlTimeZone 클래스의 메소드 중 하나로, 지정된 시간대 이름 또는 ID를 사용하여 DateTimeZone 객체를 반환합니다. 이 메소드는 PHP 7.1.0 버전부터 사용할 수 있습니다.

사용법


`IntlTimeZone::toDateTimeZone` 메소드는 두 가지 형태로 사용할 수 있습니다.

# 1. 시간대 이름을 사용하는 경우


#hostingforum.kr
php

$timezone = IntlTimeZone::toDateTimeZone('Asia/Seoul');



# 2. 시간대 ID를 사용하는 경우


#hostingforum.kr
php

$timezone = IntlTimeZone::toDateTimeZone('UTC');



예제


#hostingforum.kr
php

// 시간대 이름을 사용하여 DateTimeZone 객체를 생성합니다.

$seoul_timezone = IntlTimeZone::toDateTimeZone('Asia/Seoul');

echo $seoul_timezone->getName() . "
"; // Asia/Seoul



// 시간대 ID를 사용하여 DateTimeZone 객체를 생성합니다.

$utc_timezone = IntlTimeZone::toDateTimeZone('UTC');

echo $utc_timezone->getName() . "
"; // UTC



// DateTimeZone 객체를 사용하여 현재 시간을 가져옵니다.

$current_time = new DateTime('now', $seoul_timezone);

echo $current_time->format('Y-m-d H:i:s') . "
"; // 현재 시간



// DateTimeZone 객체를 사용하여 특정 시간을 가져옵니다.

$specific_time = new DateTime('2022-01-01 12:00:00', $utc_timezone);

echo $specific_time->format('Y-m-d H:i:s') . "
"; // 2022-01-01 12:00:00



참고


* 시간대 이름은 IANA 시간대 이름을 사용합니다. 예를 들어, 'Asia/Seoul'은 한국 시간대입니다.
* 시간대 ID는 시간대 이름과 다르게 사용됩니다. 예를 들어, 'UTC'은 UTC 시간대입니다.
* DateTimeZone 객체를 사용하여 현재 시간이나 특정 시간을 가져올 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 439 페이지

검색

게시물 검색