date
Formats a Unix timestamp as a string using the script's default timezone.
date(string $format, ?int $timestamp = null): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$format | string | The output format. Every recognised letter is a placeholder; the same set of characters accepted by DateTimeInterface::format(). |
$timestampoptional | ?int= null | A Unix timestamp in seconds. When omitted or null the current time is used. |
Return value
stringThe formatted date string, built from the timestamp interpreted in the current default timezone.
Errors
Does not throw. It emits an E_WARNING on every call if the default timezone is not a valid identifier, and an unrecognised format character is copied to the output unchanged.
Changelog
- PHP 8.0The timestamp parameter became nullable. Passing null now means the current time; in PHP 7 an explicit null was cast to 0 and produced 1 January 1970.
date turns an integer timestamp into human-readable text. Called with only a format it uses the current time, which is why date('Y-m-d') is the standard one-liner for "today".
When to reach for it
Use date for one-shot output: a log line, a filename stamp, a column header, an ISO 8601 string for a JSON payload. The moment you need arithmetic ("three months from now"), comparison, or a timezone that differs per user, move to DateTimeImmutable. A timestamp is just a number of seconds and carries no timezone of its own; a DateTimeImmutable carries one with it.
Escaping literal letters
Every recognised letter in the format is a placeholder, so printing a literal letter means prefixing it with a backslash. Use single-quoted PHP strings for formats — in a double-quoted string the backslash is consumed by PHP before date ever sees it.
Timezones
The same timestamp renders as different text depending on date.timezone or the last call to date_default_timezone_set. Set the timezone explicitly during bootstrap; relying on the server default is how a report ends up an hour off after a deployment.
Example
When to use it
- Rendering a stored Unix timestamp as a readable date in a report or email
- Building a dated filename such as backup-2025-07-04.sql
- Producing an ISO 8601 or RFC 2822 string for an API request body
Frequently asked questions
What is the difference between date and gmdate?
gmdate always formats in UTC, while date uses the script's default timezone. Given the same timestamp they only agree when the default timezone is UTC.
How do I get the current date in PHP?
Call date with just a format, for example date('Y-m-d'), which uses the current time. Set the default timezone first so the answer matches the timezone you intend.
Why does date show the wrong hour?
Almost always because the default timezone is not what you expect — it comes from date.timezone in php.ini and falls back to UTC. Call date_default_timezone_set at bootstrap to make it explicit.
More examples
Escaping literal characters
Each backslashed letter is printed literally, so the first line reads as a sentence instead of expanding I, t, i and s as placeholders. The second line is the ISO 8601 form, where the T separator must be escaped.
<?php
date_default_timezone_set('UTC');
$ts = mktime(9, 5, 0, 3, 1, 2025);
echo date('\I\t \i\s l \a\t g:ia', $ts), "\n";
echo date('Y-m-d\TH:i:sP', $ts), "\n";
echo date('Y-m-d', $ts), "\n";Format characters that answer calendar questions
t, L, N and W replace hand-written calendar arithmetic. February 2024 has 29 days and L reports 1, confirming the leap year.
<?php
date_default_timezone_set('UTC');
$ts = mktime(0, 0, 0, 2, 10, 2024);
echo 'days in this month: ', date('t', $ts), "\n";
echo 'leap year: ', date('L', $ts), "\n";
echo 'ISO weekday (1=Mon): ', date('N', $ts), "\n";
echo 'ISO week number: ', date('W', $ts), "\n";
echo 'first day of month: ', date('D', mktime(0, 0, 0, 2, 1, 2024)), "\n";The same instant in two timezones
One timestamp, three different strings. This is why a timestamp alone is not a date — the default timezone decides what date() prints.
<?php
$ts = 1751630400; // 2025-07-04 12:00:00 UTC
date_default_timezone_set('UTC');
echo date('Y-m-d H:i T', $ts), "\n";
date_default_timezone_set('Asia/Tokyo');
echo date('Y-m-d H:i T', $ts), "\n";
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i T', $ts), "\n";
Discussion