DateTime Sınıfının createFromImmutable Metodu
DateTime sınıfının statik metodu createFromImmutable, değişmez bir DateTimeImmutable nesnesi temel alarak yeni bir değiştirilebilir DateTime nesnesi oluşturur.
Sözdizimi
DateTime::createFromImmutable(DateTimeImmutable $object): DateTime
Örnek
Değişmez bir nesneden değiştirilebilir bir DateTime nesnesi oluşturalım:
<?php
$immutable = new DateTimeImmutable('2023-07-15');
$mutable = DateTime::createFromImmutable($immutable);
echo $mutable->format('Y-m-d');
?>
Kodun çalıştırılmasının sonucu:
'2023-07-15'
Örnek
Değiştirilebilir ve değişmez nesneler arasındaki farkı gösterelim:
<?php
$immutable = new DateTimeImmutable('2023-07-15');
$mutable = DateTime::createFromImmutable($immutable);
$mutable->modify('+1 day');
$newImmutable = $immutable->modify('+1 day');
echo $mutable->format('Y-m-d') . "\n";
echo $newImmutable->format('Y-m-d');
?>
Kodun çalıştırılmasının sonucu:
'2023-07-16'
'2023-07-16'
Örnek
Zaman dilimine sahip değiştirilebilir bir nesne oluşturma:
<?php
$timezone = new DateTimeZone('Europe/Moscow');
$immutable = new DateTimeImmutable('now', $timezone);
$mutable = DateTime::createFromImmutable($immutable);
echo $mutable->format('Y-m-d H:i:s e');
?>
Kodun çalıştırılmasının sonucu:
'2023-07-15 14:30:00 Europe/Moscow'
Ayrıca Bakınız
-
DateTimeImmutablesınıfı,
değişmez tarih ve saati temsil eder