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 echo date("Y-m-d", 86400); // 1970-01-02
Try it
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.
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
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.
mis the month andithe minutes;Mis the abbreviated month name.His 24-hour,his 12-hour. Mixing upY/y,d/jandm/nis the most common mistake. - Default time zone. If
date.timezoneis 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 reachdate()untouched. In double quotes, PHP expands sequences like\nand\tfirst, 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, useDateTimeImmutable. - Integers only. Microseconds are not available; the
ucharacter always gives000000.DateTime::format("u")has real microseconds. - Constants.
DATE_ATOM,DATE_RFC2822,DATE_COOKIEand friends are ready-made format strings.
Related
time(),mktime(),strtotime(), Date format characters,DateTime- Lessons: Dates and Times