YogeshChauhan . com

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.

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to load variables, functions, and mixins from another module in SCSS?SCSSWhat is iFrame in HTML? Why do we need it?HTMLHow to set opacity or transparency using CSS?CSSWhat is ECMAScript?JavaScriptJavaScript: Methods for HTML DOM classList PropertyJavaScriptCasting in PostgreSQLPostgres