date
Description
The date Date / Time for PHP format a local time/date.
Syntax
date ( string $format [, int $timestamp = time() ] ) : string
Parameters
format
The format of the outputted date string. See the formatting options below. There are also several predefined date constants that may be used instead, so for example DATE_RSS contains the format string 'D, d M Y H:i:s'.
Character | Description | Example |
---|---|---|
Day | ||
d | Day of the month, 2 digits with leading zeros | 01 - 31 |
D | A textual representation of a day, three letters | Mon - Sun |
j | Day of the month without leading zeros | 1 - 31 |
l (lowercase L) | A full textual representation of the day of the week | Sunday - Saturday |
N | ISO-8601 numeric representation of the day of the week | 1 - 7 (Monday - Sunday) |
S | English ordinal suffix for the day of the month, 2 characters | st, nd, rd, th |
w | Numeric representation of the day of the week | 0 - 6 (Sunday - Saturday) |
z | Day of the year | 0 - 365 |
Week | ||
W | ISO-8601 week number of year, weeks starting on Monday | 1 - 52 |
Month | ||
F | A full textual representation of a month | January - December |
m | Numeric representation of a month, with leading zeros | 01 - 12 |
M | A short textual representation of a month, three letters | Jan - Dec |
n | Numeric representation of a month, without leading zeros | 1 - 12 |
t | Number of days in the given month | 28 - 31 |
Year | ||
L | Whether it's a leap year | 1 (leap year), 0 (otherwise) |
o | ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. | Examples: 1999 or 2003 |
Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
y | A two digit representation of a year | 00 - 99 |
Time | ||
a | Lowercase Ante meridiem and Post meridiem | am, pm |
A | Uppercase Ante meridiem and Post meridiem | AM, PM |
B | Swatch Internet time | 000 - 999 |
g | 12-hour format of an hour without leading zeros | 1 - 12 |
G | 24-hour format of an hour without leading zeros | 0 - 23 |
h | 12-hour format of an hour with leading zeros | 01 - 12 |
H | 24-hour format of an hour with leading zeros | 00 - 23 |
i | Minutes with leading zeros | 00 - 59 |
s | Seconds with leading zeros | 00 - 59 |
u | Microseconds. Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds. | Example: 654321 |
v | Milliseconds. Same note applies as for u. | Example: 654 |
Timezone | ||
e | Timezone identifier | Examples: UTC, GMT, Atlantic/Azores |
I (uppercase i) | Whether or not the date is in daylight saving time | 1 (daylight saving time), 0 (otherwise) |
O | Difference to Greenwich time (GMT) without colon between hours and minutes | Example: +0200 |
P | Difference to Greenwich time (GMT) with colon between hours and minutes | Example: +02:00 |
T | Timezone abbreviation | Examples: EST, MDT ... |
Z | Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | -43200 - 50400 |
Full Date/Time | ||
c | ISO 8601 date | 2004-02-12T15:19:21+00:00 |
r | RFC 2822 formatted date | Example: Thu, 21 Dec 2000 16:01:07 +0200 |
U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | See also time() |
Unrecognized characters in the format string will be printed as-is. The Z format will always return 0 when using gmdate(). Note: Since this function only accepts integer timestamps the u format character is only useful when using the date_format() function with user based timestamps created with date_create().
timestamp
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
Return
Returns a formatted date string. If a non-numeric value is used for timestamp, FALSE is returned and an E_WARNING level error is emitted.
Examples
1 · format
<? $format = "Y-m-d H:i:s"; $return = date($format); echo $return; ?>
2024-03-19 01:31:23
2 · timestamp
<? $hour = 4; $minute = 5; $second = 6; $month = 2; $day = 3; $year = 2001; $format = "Y-m-d H:i:s"; $timestamp = mktime($hour, $minute, $second, $month, $day, $year); $return = date($format, $timestamp); echo $return; ?>
2001-02-03 04:05:06
3 · Constants
<? echo date(DATE_ATOM) . PHP_EOL; echo date(DATE_RSS); ?>
2024-03-19T01:31:23+00:00 Tue, 19 Mar 2024 01:31:23 +0000
4 · Characters
<? echo "day" . PHP_EOL; echo "d: " . date("d") . PHP_EOL; echo "D: " . date("D") . PHP_EOL; echo "j: " . date("j") . PHP_EOL; echo "l: " . date("l") . PHP_EOL; echo "N: " . date("N") . PHP_EOL; echo "S: " . date("S") . PHP_EOL; echo "w: " . date("w") . PHP_EOL; echo "z: " . date("z") . PHP_EOL . PHP_EOL; echo "week" . PHP_EOL; echo "W: " . date("W") . PHP_EOL . PHP_EOL; echo "month" . PHP_EOL; echo "F: " . date("F") . PHP_EOL; echo "m: " . date("m") . PHP_EOL; echo "M: " . date("M") . PHP_EOL; echo "n: " . date("n") . PHP_EOL; echo "t: " . date("t") . PHP_EOL . PHP_EOL; echo "year" . PHP_EOL; echo "L: " . date("L") . PHP_EOL; echo "o: " . date("o") . PHP_EOL; echo "Y: " . date("Y") . PHP_EOL; echo "y: " . date("y") . PHP_EOL . PHP_EOL; echo "time" . PHP_EOL; echo "a: " . date("a") . PHP_EOL; echo "A: " . date("A") . PHP_EOL; echo "B: " . date("B") . PHP_EOL; echo "g: " . date("g") . PHP_EOL; echo "G: " . date("G") . PHP_EOL; echo "h: " . date("h") . PHP_EOL; echo "H: " . date("H") . PHP_EOL; echo "i: " . date("i") . PHP_EOL; echo "s: " . date("s") . PHP_EOL; echo "u: " . date("u") . PHP_EOL; echo "v: " . date("v") . PHP_EOL . PHP_EOL; echo "timezone" . PHP_EOL; echo "e: " . date("e") . PHP_EOL; echo "I: " . date("I") . PHP_EOL; echo "O: " . date("O") . PHP_EOL; echo "P: " . date("P") . PHP_EOL; echo "T: " . date("T") . PHP_EOL; echo "Z: " . date("Z") . PHP_EOL . PHP_EOL; echo "full date/time" . PHP_EOL; echo "c: " . date("c") . PHP_EOL; echo "r: " . date("r") . PHP_EOL; echo "U: " . date("U"); ?>
day d: 19 D: Tue j: 19 l: Tuesday N: 2 S: th w: 2 z: 78 week W: 12 month F: March m: 03 M: Mar n: 3 t: 31 year L: 1 o: 2024 Y: 2024 y: 24 time a: am A: AM B: 105 g: 1 G: 1 h: 01 H: 01 i: 31 s: 23 u: 000000 v: 000 timezone e: UTC I: 0 O: +0000 P: +00:00 T: UTC Z: 0 full date/time c: 2024-03-19T01:31:23+00:00 r: Tue, 19 Mar 2024 01:31:23 +0000 U: 1710811883
5 · Escape
<? echo date('l \t\h\e jS \of F'); ?>
Tuesday the 19th of March
Links
Date / Time
- checkdate
- date_add
- date_create
- date_create_from_format
- date_create_immutable
- date_create_immutable_from_format
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse
- date_parse_from_format
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get