2024 年日本 PHP 研討會

DateTimeImmutable::createFromInterface

(PHP 8)

DateTimeImmutable::createFromInterface傳回新的 DateTimeImmutable 物件,封裝給定的 DateTimeInterface 物件

說明

public static DateTimeImmutable::createFromInterface(DateTimeInterface $object): DateTimeImmutable

參數

object (物件)

需要轉換成不可變版本的 DateTimeInterface 物件。此物件本身不會被修改,而是會建立一個新的 DateTimeImmutable 物件,其中包含相同的日期、時間和時區資訊。

回傳值

返回一個新的 DateTimeImmutable 實例。

範例

範例 #1 建立一個不可變的日期時間物件

<?php
$date
= new DateTime("2014-06-20 11:45 Europe/London");

$immutable = DateTimeImmutable::createFromInterface($date);

$date = new DateTimeImmutable("2014-06-20 11:45 Europe/London");
$also_immutable = DateTimeImmutable::createFromInterface($date);
?>

新增註記

使用者貢獻的註記

此頁面沒有使用者貢獻的註記。
To Top