Dates and time
| time and date | |
| Format with DateTimeImmutable | |
| Modify dates | |
| Compare two dates |
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.
echo time(); echo date("Y-m-d H:i:s");
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.
$now = new DateTimeImmutable("now", new DateTimeZone("UTC")); echo $now->format("Y-m-d H:i:s");
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.
$now = new DateTimeImmutable("2026-10-05", new DateTimeZone("UTC")); $later = $now->modify("+3 days"); echo $later->format("Y-m-d");
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.
$a = new DateTimeImmutable("2026-10-05"); $b = new DateTimeImmutable("2026-10-06"); var_dump($a < $b); var_dump($a->getTimestamp() < $b->getTimestamp());
Next: JSON in PHP
Article author: Arthur Isaev