W3docs

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(), and mktime() belong here.
  • The object-oriented DateTime APIDateTime, DateTimeImmutable, DateTimeZone, and DateInterval — 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, 2023

The format string is built from single-character placeholders. The most common ones:

CharMeaningExample
Y4-digit year2023
mMonth, zero-padded10
dDay of month, zero-padded25
HHour, 24h, zero-padded14
iMinutes, zero-padded30
sSeconds, zero-padded00
lFull weekday nameWednesday
FFull month nameOctober

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-31

mktime() 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-25

DateTime 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-26

The 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, 2023

Date 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 apart

Choosing 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 for false).
  • Doing arithmetic, diffs, or timezone conversions? Use DateTimeImmutable with DateInterval.

Common gotchas

  • Unset timezone. Without date_default_timezone_set() or a php.ini value, results vary by server. Set it once, early.
  • strtotime() returns false for unrecognized strings — it never throws, so always validate.
  • DateTime is mutable. modify() mutates the original; reach for DateTimeImmutable to 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().

Practice

Practice
What does the PHP 'date' function do?
What does the PHP 'date' function do?
Was this page helpful?