W3docs

date()

Date and time functions are essential in any programming language, including PHP. In this article, we will explore the various date and time functions in PHP

Introduction

Almost every application eventually has to display, store, or compare a date: a "published on" line, a countdown, an expiry check. This chapter explains how PHP's date and time tools work and when to reach for each one. We start with the everyday functions — date(), time(), and strtotime() — then move on to manipulating and comparing dates, and finish with the parts people most often get wrong: time zones and daylight saving time.

Working with Dates in PHP

PHP offers two complementary toolsets for dates:

  • Procedural functionsdate(), time(), mktime(), and strtotime() — which work with a Unix timestamp (a plain integer: the number of seconds since January 1, 1970 UTC). These are quick for simple formatting and arithmetic.
  • The object-oriented DateTime API — the DateTime, DateInterval, and DateTimeZone classes — which is more readable for anything involving intervals, comparisons, or time zones, and is the recommended approach for new code.

This chapter uses both so you can recognize each style in real codebases.

The date() Function

The date() function turns a timestamp into a formatted string. It takes a format string as its first parameter and an optional timestamp as its second; when the timestamp is omitted, date() uses the current time.

php— editable, runs on the server

This outputs the current date in the format "Month Day, Year", like March 3, 2023.

Each letter in the format string is a placeholder. The table below lists the characters you will use most often:

CharacterMeaningExample
Y4-digit year2023
y2-digit year23
mMonth, 2 digits (01–12)03
nMonth, no leading zero3
FFull month nameMarch
MShort month nameMar
dDay of month, 2 digits03
jDay of month, no leading zero3
lFull weekday nameFriday
HHour, 24-hour, 2 digits14
hHour, 12-hour, 2 digits02
iMinutes, 2 digits09
sSeconds, 2 digits05
AAM / PMPM

Any character that is not a recognized placeholder is printed literally, so date("Y-m-d H:i:s") produces a value like 2023-03-03 14:09:05. To print a literal letter that would otherwise be a placeholder, escape it with a backslash: date('l \t\h\e jS'). For the full list of placeholders, see the date-format() chapter.

The time() Function

The time() function returns the current Unix timestamp — the number of seconds elapsed since January 1, 1970 UTC. It takes no arguments and is the usual starting point for date arithmetic, because integers are easy to add to and compare.

php— editable, runs on the server

This outputs the current Unix timestamp, a long integer like 1646369616. Combine it with date() to format "now": date("Y-m-d", time()). See the time() chapter for more detail.

The strtotime() Function

The strtotime() function parses an English textual date — "March 3, 2023", "next Monday", "+2 weeks" — and returns the matching Unix timestamp. It is remarkably flexible, which makes it handy for turning human input into a timestamp you can format or compare.

php— editable, runs on the server

This outputs the Unix timestamp for March 3, 2023: 1677801600.

Gotcha: if strtotime() cannot parse the string, it returns false, not an error. Always check the result before using it — if (($ts = strtotime($input)) === false) { /* invalid date */ } — otherwise false is silently coerced to 0, which formats as January 1, 1970. See the strtotime() chapter for the full grammar it accepts.

Manipulating Dates in PHP

In addition to formatting dates, PHP also provides a variety of functions for manipulating dates. These functions allow you to add or subtract time from a date, compare dates, and more.

Adding and Subtracting Time with strtotime()

strtotime() does more than parse fixed dates — it also understands relative expressions. Pass a base timestamp as the second argument and a phrase like "+1 day", "-3 weeks", or "first day of next month" as the first, and you get a new timestamp back.

php— editable, runs on the server

This will output the date for one day after March 3, 2023, which is "March 4, 2023".

The DateTime::add() and DateTime::sub() Methods

The object-oriented DateTime::add() and DateTime::sub() methods add or subtract a DateInterval from a DateTime object. A DateInterval is built from an ISO 8601 duration string: P1D means "a period of 1 day", P2W is two weeks, and PT1H30M is one hour and thirty minutes (the T separates the date part from the time part).

This style is preferred for new code because the operation reads like a sentence and the object keeps track of its own time zone.

php— editable, runs on the server

This outputs the date one day after March 3, 2023: March 4, 2023. The dedicated date-add() and date-sub() chapters go deeper, and date-diff() shows how to get the interval between two DateTime objects.

Comparing Dates in PHP

Because timestamps are plain integers, you can compare two dates with the ordinary <, >, and == operators once both sides are timestamps. Convert each date string with strtotime() first.

php— editable, runs on the server

This outputs Date 1 is earlier than Date 2. For comparing DateTime objects you can use the same operators directly ($a < $b) without converting to timestamps.

Time Zones and Daylight Saving Time

The trickiest part of working with dates is making sure they refer to the same moment across regions. date() and time() interpret timestamps in PHP's configured default time zone, so getting that setting right matters.

The date_default_timezone_set() Function

date_default_timezone_set() sets the default time zone used by every date and time function for the rest of the request. Call it once, early in your script, and pass a valid IANA identifier such as America/New_York or Europe/London.

<?php
    date_default_timezone_set("America/New_York");
    echo date("Y-m-d H:i:s");
?>

A list of valid identifiers lives in the php-timezones chapter; reading the current setting is covered by date-default-timezone-get().

The DateTimeZone Class

When you need different time zones in the same script — for example, showing one event time to users in several regions — set the default once is not enough. The DateTimeZone class lets a specific DateTime object carry its own zone:

<?php
    $timezone = new DateTimeZone("America/New_York");
    $date = new DateTime("2023-03-03 09:00", $timezone);
    echo $date->format("Y-m-d H:i T");
?>

This formats the date in New York time and prints the zone abbreviation (T), for example 2023-03-03 09:00 EST.

Working with Daylight Saving Time

Daylight saving time (DST) shifts the clock forward or back, so the offset for a zone like America/New_York is not fixed (EST vs EDT). PHP handles the shift for you as long as you use a named zone rather than a hard-coded offset. The date() format character I (capital i) reports whether DST is currently in effect: 1 if it is, 0 if it is not.

php— editable, runs on the server

This outputs whether or not daylight saving time is in effect for the current default time zone.

Conclusion

You now have the core toolkit for dates in PHP: date() to format, time() and strtotime() to obtain and parse timestamps, the DateTime family to add, subtract, and compare, and DateTimeZone to keep different regions straight. As a rule of thumb, reach for the procedural functions for quick one-off formatting and the object-oriented DateTime API for anything involving intervals, comparisons, or multiple time zones. For a broader walkthrough see PHP Date and Time.

Practice

Practice
What are valid formats that can be used by the date() function in PHP?
What are valid formats that can be used by the date() function in PHP?
Was this page helpful?