PHP Date() Codes
The examples below relate to timezone = GMT, date = Sun 2 Jan 2028 and to time = 07:08:09 (8 minutes and 9 seconds past 7am)
Code | Description Example result |
---|---|
a | am/pm am |
A | AM/PM AM |
B | Swatch Time (0-999) 297 |
c | ISO 8601 date 2028-01-02T07:08:09+00:00 |
d | Day of the month (01 - 31) 02 |
D | Day (Sun - Sat) Sun |
e | Timezone (GMT PST EST …) GMT |
F | Month (January - December) January |
g | Hour (1 - 12) 7 |
G | Hour (0 - 23) 7 |
h | Hour (01 - 12) 07 |
H | Hour (00 - 23) 07 |
i | Minute (00 - 59) 08 |
I | Is Daylight Saving Time (1=true 0-false) 0 |
j | Day of Month (1 - 31) 2 |
l | Day (Monday - Sunday) Sunday |
L | Is Leap Year (1=true 0=false) 0 |
m | Month (01 - 12) 01 |
M | Month (Jan - Dec) Jan |
N | ISO Weekday (1=Monday 7=Sunday) 7 |
n | Month (1-12) 1 |
o | Year for ISO Week Number 2027 |
O | GMT Offset hhmm +0000 |
P | GMT Offset hh:mm +00:00 |
p | GMT offset hh:mm or Z (Z=+00:00) Z |
r | RFC2822/RFC5322 date Sun, 2 Jan 2028 07:08:09 +0000 |
S | Day ordinal suffix (st, nd, rd or th) nd |
s | Seconds (00 - 59) 09 |
t | Days in month (28, 29, 30 or 31) 31 |
T | Timezone (GMT, EST, PST, +02 … ) GMT |
u | Microseconds (often returns 0) 000000 |
U | Unix Epoch (Seconds since 1 Jan 1970) 1830409689 |
v | Milliseconds (often returns 0) 000 |
w | Day of Week (0=Sunday 6=Saturday) 0 |
W | ISO Week Number 52 |
X | Year (4 or 5 digits) (-9999 - +19999) +2028 |
x | Year (4 or 5 digits) (-9999 - 9999 or +10000 - +19999) 2028 |
Y | Year (4 or 5 digits) (-9999 - 19999 2028 |
y | Year (2 digits) (00 - 99) 28 |
z | Day of year (0 - 365) 1 |
Z | Timezone UTC offset seconds (-43200 - 50400) 0 |
PHP getdate() Codes
Element | Description Example result |
---|---|
seconds | Seconds (0 - 59) 9 |
minutes | Minutes (0 - 59) 8 |
hours | Hours (0 - 23) 7 |
mday | Day (1 - 31) 2 |
wday | Day (0=Sunday 6=Saturday) 0 |
mon | Month (1-12) 1 |
year | Year (0000 - 9999) 2028 |
yday | Day of year (0 - 365) 1 |
weekday | Day (Sunday - Saturday) Sunday |
month | Month (January - December) January |
0 | Seconds since 1970 1830409689 |
When working with dates or times in PHP, it is important to ensure the test environment is using the same timezone as your live website server. Ignoring timezones can create strange time errors. These can suddenly appear, long after the page goes live. (Time/date timezone errors often appear on dates linked to the European or American Summer/Winter time boundaries.
Adding the line
date_default_timezone_set('xxxxx');
to the start of your PHP script will ensure consistent behaviour.
(Replace xxxxx with an appropriate timezone. The timezone Europe/London correctly adjust for UK Wintertime(GMT) and Summertime (BST).
This site is based in the UK, hence I use line
date_default_timezone_set('Europe/London');
Timezones normally consist of three parts - The name of a continent or ocean (e.g. Europe), the separator character / and the
name of the large city within the timezone (e.g. London);
A few timezones use Continent/State/City or Continent/Country