PHP Date
IntroductionPHP date functions are essential for developers when it comes to handling and displaying dates and times. In this guide, we will provide you with
Introduction
Almost every application has to read, format, or do arithmetic on dates: log entries, "posted 3 days ago" labels, scheduling, expiry checks. PHP gives you two parallel toolsets for this:
- Procedural functions built around the Unix timestamp — an integer counting seconds since January 1, 1970, 00:00:00 UTC.
time(),date(),strtotime(), andmktime()belong here. - The object-oriented
DateTimeAPI —DateTime,DateTimeImmutable,DateTimeZone, andDateInterval— which is safer for arithmetic and timezone handling.
This page covers the functions and classes you reach for most often, when to pick each one, and the timezone pitfall that trips up almost everyone.
A note on timezones
By default, PHP uses the timezone configured in php.ini (the date.timezone setting). If that isn't set, you may get inconsistent results between servers. Set it explicitly at the top of your script, or pass a DateTimeZone to each object:
date_default_timezone_set('UTC');See PHP Timezones for the full list of valid identifiers and how to convert between zones.
The date() Function
date(string $format, ?int $timestamp = null) formats a timestamp into a human-readable string. When you omit the timestamp it uses the current time. This is the workhorse for displaying dates.
echo date('Y-m-d H:i:s'); // e.g. 2023-10-25 14:30:00
echo date('l, F j, Y'); // e.g. Wednesday, October 25, 2023The format string is built from single-character placeholders. The most common ones:
| Char | Meaning | Example |
|---|---|---|
Y | 4-digit year | 2023 |
m | Month, zero-padded | 10 |
d | Day of month, zero-padded | 25 |
H | Hour, 24h, zero-padded | 14 |
i | Minutes, zero-padded | 30 |
s | Seconds, zero-padded | 00 |
l | Full weekday name | Wednesday |
F | Full month name | October |
To print a literal letter that is also a format character, escape it with a backslash: date('\T\o\d\a\y: Y-m-d').
The time() Function
time() returns the current Unix timestamp as an integer. Use it whenever you need a numeric "now" to store, compare, or do arithmetic on.
$now = time();
echo $now; // an integer such as 1698241800
// One hour from now:
echo date('Y-m-d H:i:s', $now + 3600);Because a timestamp is just an integer of seconds, you can add or subtract durations directly (+ 3600 for an hour, + 86400 for a day). For anything more complex than a few fixed offsets, prefer DateTime arithmetic below.
The strtotime() Function
strtotime(string $datetime, ?int $baseTimestamp = null) parses an English textual date/time into a timestamp, returning false on failure. It understands both absolute strings and relative phrases.
echo strtotime('2023-10-25 14:30:00'); // 1698244200 (UTC)
echo "\n";
var_dump(strtotime('next monday')); // a future timestamp, or false if unparseable$tomorrow = strtotime('+1 day');
echo date('Y-m-d', $tomorrow);Always check for false before using the result, since a typo silently fails rather than throwing.
The mktime() Function
mktime(int $hour, int $minute, int $second, int $month, int $day, int $year) builds a timestamp from individual components. Note the argument order is time-first, then date.
$timestamp = mktime(0, 0, 0, 12, 31, 2023);
echo date('Y-m-d', $timestamp); // 2023-12-31mktime() normalizes out-of-range values, which is handy: mktime(0, 0, 0, 13, 1, 2023) rolls "month 13" into January 2024.
The DateTime Class
new DateTime(string $datetime = 'now', ?DateTimeZone $timezone = null) wraps a date in an object you can format, compare, and modify with method calls. (The date_create() function is a procedural alias for the same constructor.)
$date = new DateTime('2023-10-25', new DateTimeZone('UTC'));
echo $date->format('Y-m-d'); // 2023-10-25DateTime is mutable — methods like modify() change the object in place. That's the source of subtle bugs when an object is shared, which is why the immutable version below is usually preferred.
The DateTimeImmutable Class
DateTimeImmutable has the same API as DateTime, but every modifying method returns a new object and leaves the original untouched. For modern code this is the safer default.
$date = new DateTimeImmutable('2023-10-25');
$newDate = $date->modify('+1 day');
echo $date->format('Y-m-d'); // 2023-10-25 (unchanged)
echo "\n";
echo $newDate->format('Y-m-d'); // 2023-10-26The DateTime::format() Method
format(string $format) turns a DateTime or DateTimeImmutable object into a string. It accepts the same format characters as the date() function.
$date = new DateTimeImmutable('2023-10-25 14:30:00');
echo $date->format('l, F j, Y'); // Wednesday, October 25, 2023Date arithmetic and differences
The object API shines for arithmetic. Use DateInterval (ISO-8601 duration strings) to add or subtract, and diff() to compare two dates:
$start = new DateTimeImmutable('2023-10-25');
$later = $start->add(new DateInterval('P10D')); // P10D = 10 days
echo $later->format('Y-m-d'); // 2023-11-04
$diff = $start->diff(new DateTimeImmutable('2023-12-31'));
echo "\n" . $diff->days . ' days apart'; // 67 days apartChoosing the right tool
- Need a quick formatted "now"? Use
date(). - Storing or comparing a moment numerically? Use
time()/ Unix timestamps. - Parsing user or log input? Use
strtotime()(and check forfalse). - Doing arithmetic, diffs, or timezone conversions? Use
DateTimeImmutablewithDateInterval.
Common gotchas
- Unset timezone. Without
date_default_timezone_set()or aphp.inivalue, results vary by server. Set it once, early. strtotime()returnsfalsefor unrecognized strings — it never throws, so always validate.DateTimeis mutable.modify()mutates the original; reach forDateTimeImmutableto avoid shared-state surprises.- Timestamps are UTC seconds. Formatting them with
date()applies the current timezone, so the same integer renders differently depending on your settings.
Conclusion
PHP gives you a procedural timestamp toolkit (date(), time(), strtotime(), mktime()) and an object-oriented one (DateTime, DateTimeImmutable). Lean on plain timestamps for simple display and storage, and on DateTimeImmutable for arithmetic and timezone-aware logic. For deeper dives, see PHP Date and Time, the date() reference, strtotime(), and mktime().