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 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 add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to create a placeholder loader (throbber) using CSS?CSSJavaScript: how to detect a browser using the user agent?JavaScriptLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPCSS Overflow Property with ExamplesCSSLIKE and ILIKE Operators in PostgresPostgresExplanation of PostgreSQL PgAdmin interfacePostgres