date()
date() — Format a Unix timestamp.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Returns a string formatted according to the given format string using the given integer timestamp (Unix timestamp) or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
Signature
date(string $format, ?int $timestamp = null): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $format | string | Format accepted by DateTimeInterface::format(). |
| $timestamp | ?int | The optional timestamp parameter is an int Unix timestamp that defaults to the current local time if timestamp is omitted or null. In other words, it defaults to the value of time(). |
Return value
Returns a formatted date string.
Example
A runnable snippet:
// set the default timezone to use. date_default_timezone_set('UTC'); // Prints something like: Monday echo date("l") . "\n"; // Prints something like: Monday 8th of August 2005 03:12:46 PM echo date('l jS \of F Y h:i:s A') . "\n"; // Prints: July 1, 2000 is on a Saturday echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000)) . "\n"; /* use the constants in the format parameter */ // prints something like: Wed, 25 Sep 2013 15:28:57 -0700 echo date(DATE_RFC2822) . "\n"; // prints something like: 2000-07-01T00:00:00+00:00 echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
Changelog
| Version | Change |
|---|---|
| 8.0.0 | timestamp is nullable now. |
See also
gmdate(), idate(), getdate(), getlastmod(), mktime(), time()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page date() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.