라이브러리

[PHP] DateTimeImmutable::__construct - 새 DateTimeImmutable 객체를 반환합니다.




DateTimeImmutable::__construct


PHP 8.0 버전부터 `DateTimeImmutable` 클래스가 추가되었습니다. `DateTimeImmutable` 클래스는 `DateTime` 클래스와 유사하지만, 생성된 객체를 변경할 수 없습니다. 즉, `DateTimeImmutable` 객체는 불변 객체입니다.

`DateTimeImmutable::__construct` 메서드는 `DateTimeImmutable` 객체를 생성하는 데 사용됩니다. 이 메서드는 다음과 같은 매개 변수를 받을 수 있습니다.

- `date` (string): 날짜를 나타내는 문자열입니다. 예를 들어, "2022-01-01" 또는 "2022-01-01 12:00:00"입니다.
- `time` (int): 시간을 나타내는 정수입니다. 예를 들어, 1643723400입니다.
- `timezone` (DateTimeZone): 시간대 객체입니다.

예제


#hostingforum.kr
php

// 현재 날짜와 시간을 사용하여 DateTimeImmutable 객체를 생성합니다.

$date = new DateTimeImmutable();

echo $date->format('Y-m-d H:i:s') . "
";



// 날짜를 문자열로 사용하여 DateTimeImmutable 객체를 생성합니다.

$date = new DateTimeImmutable('2022-01-01');

echo $date->format('Y-m-d') . "
";



// 시간을 정수로 사용하여 DateTimeImmutable 객체를 생성합니다.

$date = new DateTimeImmutable('@1643723400');

echo $date->format('Y-m-d H:i:s') . "
";



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

$timezone = new DateTimeZone('Asia/Seoul');

$date = new DateTimeImmutable('2022-01-01', $timezone);

echo $date->format('Y-m-d H:i:s') . "
";



참고


- `DateTimeImmutable` 클래스는 `DateTime` 클래스와 유사하지만, 생성된 객체를 변경할 수 없습니다.
- `DateTimeImmutable::__construct` 메서드는 `DateTimeImmutable` 객체를 생성하는 데 사용됩니다.
- `date`, `time`, `timezone` 매개 변수를 사용하여 `DateTimeImmutable` 객체를 생성할 수 있습니다.
- `DateTimeImmutable` 객체는 불변 객체입니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색