Tsallaka zuwa abun ciki
Justice Tech School

DateTime In Depth

DateTimeImmutable never changes in place. Use DateInterval, DatePeriod and time zones for real work. format uses letters such as Y m d H i s and D for the weekday.

Example
$d = new DateTimeImmutable("2026-03-15 09:30", new DateTimeZone("UTC"));
echo $d->format("D, d M Y H:i"), "\n";
$later = $d->add(new DateInterval("P1M2D"));
echo $later->format("Y-m-d"), "\n";
echo $d->setTimezone(new DateTimeZone("Africa/Lagos"))->format("H:i"), "\n";
foreach (new DatePeriod($d, new DateInterval("P1D"), 2) as $day) echo $day->format("md "), "";
echo "\n";

Result

Sun, 15 Mar 2026 09:30
2026-04-17
10:30
0315 0316 0317 

Exercise

Immutable date class.

new ("now")

Quiz Test

Why prefer DateTimeImmutable?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.