Yogesh Chauhan's Blog

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

in PHP on 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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to Sort (Shuffle) an Array in Random Order in JavaScript?JavaScriptHow to Secure Web or Mobile Browsers?MiscellaneousCreate a menu with a curtain falling animation using CSS and JavaScriptCSSA complete guide to add responsive YouTube videos using HTML and CSSCSSThe Difference Between Arrays and Objects in JavaScriptJavaScriptHow to modify navigation menus in WordPress?WordPress