Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Date and Time

PHP date() function

Reference for PHP date(): format a Unix timestamp with format characters like Y-m-d H:i:s, how the time zone affects output, escaping text and gmdate().

date() turns a Unix timestamp into a formatted string. You describe the layout with a format string in which each letter stands for a part of the date: Y is the four-digit year, m the month, d the day, H the hour, and so on. Characters that are not format letters are copied as they are.

Without a second argument, date() formats the current time. The result is shown in the default time zone (see date_default_timezone_set()). The full list of letters is on the date format characters page.

PHP
<?php
echo date("Y-m-d", 86400);   // 1970-01-02

Try it

PHP Common formats for a fixed timestamp

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Time zones, escaping and gmdate()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
date(string $format, ?int $timestamp = null): string
gmdate(string $format, ?int $timestamp = null): string
Name Description
$format A string of format characters. Escape a literal letter with a backslash, and remember that in a double-quoted PHP string the backslash itself must be doubled for letters that form escape sequences (\n, \t).
$timestamp Optional Unix timestamp. null or omitted means the current time.

Return value: the formatted string. gmdate() is identical but always formats in UTC.

Errors: none for odd formats, which are never rejected. Passing a non-integer timestamp such as a numeric string is coerced; a float with a fractional part triggers a deprecation in PHP 8.1 and later.

Usage notes

  • Pick the right letters. m is the month and i the minutes; M is the abbreviated month name. H is 24-hour, h is 12-hour. Mixing up Y/y, d/j and m/n is the most common mistake.
  • Default time zone. If date.timezone is not set, PHP uses UTC. Set it explicitly in your application so output does not change between servers.
  • Single quotes help. In a single-quoted format such as 'Y-m-d \a\t H:i' the backslashes reach date() untouched. In double quotes, PHP expands sequences like \n and \t first, so double the backslash.
  • No locale. Month and day names are always English. For localized names use IntlDateFormatter (the intl extension) or map names yourself.
  • Use the object API for calculations. date() only formats. For adding days, comparing time zones or parsing, use DateTimeImmutable.
  • Integers only. Microseconds are not available; the u character always gives 000000. DateTime::format("u") has real microseconds.
  • Constants. DATE_ATOM, DATE_RFC2822, DATE_COOKIE and friends are ready-made format strings.