Advertisement
Tech

Using the PHP Date() Function to Format Date in PHP

The date() function is used to format a date or time in PHP. The function can be used to print out the current year for a copyright notice, or print the seconds since the Unix epoch as well as every other date and time permeation you can think of.

By Kristen Grubb
Desk Tech
Reading time 3 min read
Word count 486
Web development Internet Php help
Using the PHP Date() Function to Format Date in PHP
Advertisement
Quick Take

The date() function is used to format a date or time in PHP. The function can be used to print out the current year for a copyright notice, or print the seconds since the Unix epoch as well as every other date and time permeation you can think of.

On this page

Format Date in PHP

The date function takes the format:

string date (string $format)

Advertisement

The $format string uses the following characters to represent different formatting options.

Day

Advertisement

d-Day of the month, 2 digits with leading zeros

D-A textual representation of a day, three letters

Advertisement

j-Day of the month without leading zeros 1 to 31

l-A full textual representation of the day of the week

Advertisement

N-ISO-8601 numeric representation of the day of the week (1 for Monday through 7 for Sunday)

S-English ordinal suffix for the day of the month

Advertisement

w-Numeric representation of the day of the week (0 for Sunday through 6 for Saturday)

z-The day of the year (starting from 0)

Advertisement

Week

W-ISO-8601 week number of year, weeks starting on Monday

Advertisement

Month

F-A full textual representation of a month, such as January or March

Advertisement

m-Numeric representation of a month, with leading zeros

M-A short textual representation of a month, three letters

Advertisement

n-Numeric representation of a month, without leading zeros

t-Number of days in the given month

Advertisement

Year

L-Whether it’s a leap year

Advertisement

o-ISO-8601 year number.

Y-A full numeric representation of a year

Advertisement

y-A two digit representation of a year

Time

Advertisement

a-Lowercase Ante meridiem and Post meridiem (am or pm)

A-Uppercase Ante meridiem and Post meridiem (AM or PM)

B-Swatch Internet time

g-12-hour format of an hour without leading zeros

G-24-hour format of an hour without leading zeros

h-12-hour format of an hour with leading zeros

H-24-hour format of an hour with leading zeros

i-Minutes with leading zeros

s-Seconds with leading zeros

u-Microseconds

Timezone

e-Timezone identifier

I-Whether or not the date is in daylight saving time

O- Difference to Greenwich time (GMT) in hours

P-Difference to Greenwich time (GMT) with colon between hours and minutes

T-Timezone abbreviation

Z-Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.

Full Date/Time

c-ISO 8601 date

r-RFC 2822 formatted date

U-Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Examples

As you can see, there are many different ways to format date in PHP. Some common examples are as follows:

If you want your date to appear as January 01, 2010, the format would be:

date(“F d, Y”);

If you prefer the date to appear as 01/01/10 (mm/dd/YY), the format would be:

date(“m/d/y”);

If you want to print out the current date and time in the format Jan 1, 2010 8:00 pm, the format would be:

date(“M j, Y h:i a”);

The format for the seconds since the Unix epoch would be:

date(“U”);

MySQL expects the date to be formatted as “YYYY-MM-DD” or 2010-01-01. The format for MySQL would be:

date(“Y-m-d”);

Conclusion

As you can see, the PHP date() function is a very useful formatting tool. Other than using it to print the current date, it can also be used to calculate the time between two dates, convert the Unix timestamp, and determine a user’s timezone.

Keep Exploring

More from Tech

Filed under
Web development Internet
More topics
Php help
Advertisement