PHP Date

You will find yourself using the date function time and time again. This is a function you should become very familiar with. There are many ways to display a date by using PHP.

Date Syntax

The following syntax is used to display the date:

date(format,timestamp)

Date Format

Below are the values you can use to format the time and date using PHP.

Credits to PHP.net (http://us3.php.net/date) for helping me with this as I do not have all these values memorized.

d = Day of the month with leading zeros
Example: 01-31

j = Day of the month without leading zeros
Example: 1-31

D = Three letter abbreviation of the day of the week
Example: Mon-Sun

l = Full name for the day of the week
Example: Monday-Sunday

N = ISO-8601 Numeric value for the day of the week
Example: 1 (Monday)- 7 (Sunday)

w = Numeric value for the day of the week
Example: 0 (Sunday)- 6 (Saturday)

S = English ordinal suffix for the day of the month
Example: st, nd, th

z = Day of the year
Example: 0-363

W = ISO-8601 week number of the year (weeks beginning on Monday)
Example: 1-52 (1 = 1st week in the year)

F = Full name of the month
Example: January-December

m = Numeric value of the month with leading zeros
Example: 01-12

n = Numeric value of the month without leading zeros
Example: 1-12

M = Three letter abbreviation of the month
Example: Jan-Dec

t = Number of days in the referenced month
Example: 28-31

L = Leap year
Example: 1 (leap year) or 0 (not a leap year)

o = ISO-8601 year number (Same as Y below, except if ISO week number, W, does not belong to the current year, that year is used.
Example: 2009

Y = Full numeric value of the year
Example: 2009

y = Two digit value of the year
Example: 09

a = Lowercase am or pm
Example: am or pm

A = Uppercase AM or PM
Example: AM or PM

B = Swatch Internet time
Example: 000-999

h = Hour in 12 hour format with leading zeros
Example: 01-12

g = Hour in 12 hour format without leading zeros
Example: 1-12

H = Hour in 24 hour format with leading zeros
Example: 00-23

G = Hour in 24 hour format without leading zeros
Example: 0-23

i = Minutes with leading zeros
Example: 00-59

s= Seconds with leading zeros
Example: 00-59

u = Microseconds
Example: 54321

e = Timezone
Example: GMT, EST

I = Daylight savings time
Example: 1 (daylight savings time) or 0 (not daylight savings time)

O = Difference to Greenwich time (GMT) in hours without colon
Example: +0400

P = Difference to Greenwich time (GMT) in hours with colon
Example: 04:00

T = Timezone abbreviation
Example: EST, MDT

Z = Timezone offset from UTC (west of UTC is negative, east of UTC is positive)
Example: -43200-50400

c = ISO 8601 complete date
Example: 2009-01-31T13:30:00 +0300

r = Formatted date
Example: Sat, 31 Jan 2009 13:30:00 +0300

Displaying The Date

By utilizing the date and time formats above, we can display the date in a wide variety of ways.

Usually, additional characters are used to display the date in a more user friendly way. For example, the characters “/”, “.”, and “-” are used quite often when displaying the date using PHP.

An example of displaying the date can look like:

echo “The current date is:<br />”;
echo date(“m/d/Y”);

Assuming the date is January 1, 2009, this bit of code would display:

The current date is:
01/01/2009

Alternatively, we can use something like:

echo “It is now:<br />”;
echo date(“F j, Y g:i A”);

Assuming the date is January 1, 2009 and the time is 12:05pm, this bit of code would display:

It is now:
January 1, 2009 12:05 PM