To work with dates and times in PHP, use the DateTimeImmutable class for anything beyond a quick timestamp, and use date() and time() for simple formatting of the current moment. The php date time toolkit covers four jobs: getting the current time, formatting it, parsing text into a date, and doing arithmetic such as "add 30 days". This guide shows the procedural functions first, then the object API you should prefer in modern PHP 8.x code.
Dates look simple until you meet month ends, leap years and daylight saving. Letting PHP's built-in classes do that work is always safer than adding seconds by hand.
time() returns the current Unix timestamp: the number of seconds since 1 January 1970 00:00:00 UTC. A timestamp is just an integer, so it is easy to store and compare.
date() turns a timestamp into a formatted string using the server's default time zone. Without a second argument it formats the current time.
The exact numbers depend on when you run it. The format characters are letters, and any letter PHP does not recognise is printed as is, so escape literal text with a backslash.
The same format characters are used by date(), DateTime::format() and DateTimeImmutable::format().
| Character | Meaning | Example |
|---|---|---|
Y | Four digit year | 2026 |
y | Two digit year | 26 |
m | Month with leading zero | 03 |
n | Month without leading zero | 3 |
F / M | Full / short month name | November / Nov |
d | Day of month with leading zero | 07 |
j | Day of month without leading zero | 7 |
l / D | Full / short weekday name | Tuesday / Tue |
H / h | Hour 24-hour / 12-hour | 14 / 02 |
i | Minutes | 05 |
s | Seconds | 09 |
A | AM or PM | PM |
T / e | Time zone abbreviation / identifier | CET / Europe/Paris |
c | ISO 8601 date | 2026-11-17T09:30:00+00:00 |
For machine-to-machine data, prefer ISO 8601 (c or the constant DateTimeInterface::ATOM) because every language can parse it.
DateTimeImmutable is an object that holds one moment in time. Every change returns a new object and leaves the original untouched, which prevents the classic bug where a helper function silently modifies a date you still need. The older DateTime class has the same methods but mutates itself.
The output surprises many people: 31 January plus one month is "31 February", which does not exist, so PHP rolls over into March. If you want the last day of the next month, ask for it explicitly with $start->modify('last day of next month').
new DateTimeImmutable() with no argument means "now". Passing a string such as 'tomorrow noon' or 'next monday' also works because the constructor understands PHP's relative date formats.
There are two parsers, and the difference matters.
strtotime() guesses the format and returns a timestamp, or false on failure.DateTimeImmutable::createFromFormat() demands an exact format and returns false when the text does not match.Always prefer createFromFormat() for user input. A string like 03/04/2026 means 3 April in most of the world and 4 March in the United States, and strtotime() picks one silently. Since PHP 8.2, getLastErrors() returns false when there were no warnings or errors, so check for false first, as above. A value such as 31/02/2026 is parsed but produces a warning, because PHP rolls it forward to March.
A DateInterval describes a span such as "3 days" or "1 year and 2 hours". You build it from an ISO 8601 duration string: P starts a period, T separates date parts from time parts.
Prefer add() and sub() over adding 86400 * $days seconds to a timestamp. A day is not always 86,400 seconds in a time zone that changes its clocks.
You can compare two date objects with <, > and ==. To measure the gap, use diff(), which returns a DateInterval.
The days property is the total number of days between the dates, while d in format() is only the leftover days after whole months are counted. Use days when you need a plain day count.
Walking through a range is easy with DatePeriod:
DateTime changes itself when you call modify(), add() or setDate(), while DateTimeImmutable returns a new object each time. Immutable objects are safer to pass around, so use them by default.
date() uses the default time zone, which comes from the date.timezone setting or date_default_timezone_set(). If your server is in UTC and your users are not, the output looks off. A related article covers time zones in depth.
Use date('Y') and date('n'), or (new DateTimeImmutable())->format('Y'). Calling format() repeatedly on one object avoids a rare mismatch if the clock ticks over midnight between calls.
Store timestamps or ISO 8601 strings in UTC, and format them only when you display them. Both sort correctly and avoid ambiguity.
Use time() and date() for quick jobs, and DateTimeImmutable for real work. Parse user input with createFromFormat() and check for false. Do arithmetic with add(), sub(), modify() and DateInterval, never by multiplying seconds, and measure gaps with diff(). Next you will see how time zones change what these objects mean.