Here’s what we can do with PHP date() function

February 26, 2021

The PHP date() function formats a date-time and returns the requested date/time strings.

Let’s see what we can do with it.

All you have to do is pass the small letter ‘d’ to get the day of the month for the current day. It will print the day according to your timezone.

echo date("d"); //26

echo date("j"); //26

echo date("D"); //Fri

echo date("l"); //Friday

Print the numeric representation of the day of the week for today’s date

N will print the ISO-8601 numeric representation of a day. This will print 1 for Sunday, 2 for Monday, 7 for Saturday etc.

echo date("N"); //5

echo date("w"); //5

Print the English ordinal suffix for the day of the month for today’s date

echo date("S"); //th

The date(‘S’) alone doesn’t make any sense but we can use it with ‘j’ and it will gives the complete number of the day of the month

echo date("jS"); //26th

It prints the day of the year between 0 to 365/366 depending on the year.

echo date("z"); //56

echo date("W"); //08

echo date("F"); //February

echo date("M"); //Feb

It will print the number of the month with leading zeros.

echo date("m"); //02

There is another letter to print month number without leading zeros.

echo date("n"); //2

echo date("t"); //28

Check if the current year is a leap year

It returns 1 if it is a leap year and 0 if it’s not a leap year.

echo date("L"); //0

echo date("o"); //2021
echo date("Y); //2021

echo date("y"); //21

echo date("a"); //am
echo date("A"); //AM

g will print the hour without leading zeros and h will add the leading zero.

echo date("g"); //3
echo date("h"); //03

G will print the hour without leading zeros and H will add the leading zero.

echo date("G"); //15
echo date("H"); //15

echo date("i"); //14
echo date("s"); //19

echo date("e"); //UTC

Check if the date is in daylights savings time

I will return 1 if the date is in Daylight Savings Time, otherwise it will return 0

echo date("I"); //0

Print the difference between the current time and the Greenwich time (GMT) in hours

echo date("O"); //+0000

If you want the difference with minutes then use P

echo date(")"); //+00:00

echo date("T"); //UTC

echo date("Z"); //0

echo date("c"); //2021-02-26T03:26:32+00:00

The Unix Epoch time is January 1 1970 00:00:00 GMT. This code will print the time in seconds since that day and time.

echo date("U"); //1614309992

There is still so much we can do with PHP date function. Checkout the PHP official guide to lean more.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

Clearwater Seafoods – B2C in ChinaMiscHow to use GROUPING SETS to boost GROUP BY queries in Postgres?PostgresSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscSteps to Secure a VPNMiscHow to create a cross-browser smooth scrolling with jQuery?jQueryLIKE and ILIKE Operators in PostgresPostgres