Dates and time
PHP holds a moment in time as a whole number of seconds and prints it through a pattern. The two tools of this lesson are the old pair of functions and the immutable date object.
time and date
Function time returns the current moment as a whole number. Function
date formats that moment with a pattern like Y-m-d.
Start with the guide
Date and Time in PHP
for the full list of patterns.
<?php echo time(), PHP_EOL; echo date("Y-m-d H:i:s"), PHP_EOL;
php now.php
Both lines depend on the clock, so no fixed output is printed here: the first number is the count of seconds since 1 January 1970, and the second line is the same moment written out.
Format with DateTimeImmutable
DateTimeImmutable keeps a fixed point in time and never changes itself. Call format with a pattern to render year, month, day and time.
Set the zone explicitly so results stay stable on any server. See also
timezones
for zone names.
<?php $moment = new DateTimeImmutable("2026-10-05 12:00:00", new DateTimeZone("UTC")); echo $moment->format("Y-m-d H:i:s"), PHP_EOL;
php format.php
2026-10-05 12:00:00
The pattern Y-m-d gives four digits of year, two of month and two of day, and H:i:s adds the time in 24-hour form. The zone is part of the object, so the same moment prints the same line on every server.
Modify dates
The modify method returns a new object and leaves the original unchanged.
Pass expressions like +3 days or next Monday to move forward. Chain format after modify to display the result.
<?php $now = new DateTimeImmutable("2026-10-05", new DateTimeZone("UTC")); $later = $now->modify("+3 days"); echo $later->format("Y-m-d"), PHP_EOL; echo $now->format("Y-m-d"), PHP_EOL;
php modify.php
2026-10-08
2026-10-05
The first line is the moved date and the second one proves the original still holds the fifth, because modify handed back a new object instead of editing the old one.
Compare two dates
Compare objects directly to find the earlier date. You can also compare timestamps when you need numbers.
The tutorial
Compare two dates
shows more cases step by step.
<?php $a = new DateTimeImmutable("2026-10-05"); $b = new DateTimeImmutable("2026-10-06"); var_dump($a < $b); var_dump($a->getTimestamp() < $b->getTimestamp()); var_dump($a->diff($b)->days);
php compare.php
bool(true)
bool(true)
int(1)
The object comparison and the timestamp comparison agree that the fifth comes before the sixth, and diff reports the distance between them as one day.
Exercises
- Print the timestamp of 2026-10-05 00:00:00 in UTC by passing it to strtotime and then to date.
- Format the same moment as d.m.Y and as l, j F Y. Which part of the output changes?
- Move the date of modify.php back one month with -1 month. Which day does the result show, and why?
- Compare two dates that are the same instant in two different zones. What does the comparison say, and why?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
time()return?Answer
The number of seconds since 1 January 1970
time gives the Unix timestamp, a whole number of seconds; date turns it into a readable string.
-
What does
DateTimeImmutableguarantee?Answer
That a change returns a new object and the original stays as it was
Every modifying method returns a new object, which is what the immutable in the name promises.
-
What does the pattern
Y-m-dprint for 5 October 2026?Answer
2026-10-05
Y is the four-digit year, m the two-digit month and d the two-digit day, in that order.
-
Why should a date object get an explicit time zone?
Answer
So the same moment prints the same line on any server
Without a zone PHP uses the server default, and the same timestamp can then print a different hour elsewhere.
-
What does this program print?
<?php $d = new DateTimeImmutable("2026-10-05"); echo $d->format("Y-m-d");Answer
2026-10-05
The pattern writes the year, the month and the day of the object in that order.
-
What does this program print?
<?php $d = new DateTimeImmutable("2026-10-05"); echo $d->modify("+3 days")->format("Y-m-d");Answer
2026-10-08
Three days after the fifth of October is the eighth, and modify returns the moved object.
-
What does this program print?
<?php $d = new DateTimeImmutable("2026-10-05"); echo $d->modify("+1 month")->format("Y-m-d");Answer
2026-11-05
One month after 5 October is 5 November, and the day stays the same.
-
What does this program print?
<?php $a = new DateTimeImmutable("2026-10-05"); $b = new DateTimeImmutable("2026-10-06"); var_dump($a < $b);Answer
bool(true)
Date objects compare by the moment they hold, and the fifth is earlier than the sixth.
-
What does this program print?
<?php $d = new DateTimeImmutable("2026-10-05 12:30:00"); echo $d->format("H:i");Answer
12:30
H is the 24-hour hour and i the minutes, so the pattern gives 12:30.
-
What does
$a->diff($b)->daysgive you?Answer
The number of days between the two dates
diff returns an interval, and its days property holds the distance between the two moments in days.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev