Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

DateTimeImmutable

Keep an original date while deriving adjusted dates.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new DateTimeImmutable($datetime, $timezone)

Keep an original date while deriving adjusted dates. Modifier methods such as add return a new object; discarding that result leaves the variable unchanged. A calendar day across a daylight-saving transition need not equal 86,400 elapsed seconds.

$zone=new DateTimeZone("Europe/Warsaw");
$a=new DateTimeImmutable("2024-03-30 12:00",$zone); $b=$a->add(new DateInterval("P1D"));
echo json_encode([$a->format("d H:i P"),$b->format("d H:i P"),$b->getTimestamp()-$a->getTimestamp()]);
["30 12:00 +01:00","31 12:00 +02:00",82800]

Examples

<?php
$zone=new DateTimeZone("Europe/Warsaw");
$a=new DateTimeImmutable("2024-03-30 12:00",$zone); $b=$a->add(new DateInterval("P1D"));
echo json_encode([$a->format("d H:i P"),$b->format("d H:i P"),$b->getTimestamp()-$a->getTimestamp()]);

Result

["30 12:00 +01:00","31 12:00 +02:00",82800]

Sources