The PHP date() function formats a date-time and returns the requested date/time strings.
Let’s see what we can do with it.
Print the day of the month for today’s date
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
Print the day of the month without leading zeros for today’s date
echo date("j"); //26
Print the day of the month in 3 letters for today’s date
echo date("D"); //Fri
Print the day of the month in full name for today’s date
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
Print the day of the year
It prints the day of the year between 0 to 365/366 depending on the year.
echo date("z"); //56
Print the number of week in the year
echo date("W"); //08
Print the current month in full
echo date("F"); //February
Print the current month in 3 letters
echo date("M"); //Feb
Print the current month number in a year
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
Print the number of days in current month
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
Print the current year in 4 digits
echo date("o"); //2021 echo date("Y); //2021
Print the current year in 2 digits
echo date("y"); //21
Print AM in uppercase or lowercase
echo date("a"); //am echo date("A"); //AM
Print 12-hour format of an hour
g will print the hour without leading zeros and h will add the leading zero.
echo date("g"); //3 echo date("h"); //03
Print 24-hour format of an hour
G will print the hour without leading zeros and H will add the leading zero.
echo date("G"); //15 echo date("H"); //15
Print Minutes and Seconds with leading zeros
echo date("i"); //14 echo date("s"); //19
Print the current timezone
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
Print the timezone abbreviations
echo date("T"); //UTC
Print the timezone offset in seconds
echo date("Z"); //0
Print the complete ISO-8601 date
echo date("c"); //2021-02-26T03:26:32+00:00
Print the seconds since the Unix Epoch
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.
date functions time