New way to learn PHP

PHP 5 Date and time

Working with date and time function

  • Web applications often deal with date and time, like current date.
  • PHP deal with date arithmetic that make manipulating dates easily.
  • Like date of accessing websites.
  • Like print current date.

PHP provides date () function to retrieve all the information about the date and time with the help of parameters like "d" for the day.

Print current date using date ()


  	echo date ("l")."
"; echo date ("l dS F Y h:i:s A")."
"; echo date ("l \\t\h\e jS")."
"; ?>

In the above example, we are printing the current date in different format like only day or day with month with the help of the parameter.

Syntax of date function

date (format, timestamp); 

String as per your requirement.


Timestamp is optional.


Format Description Example
d Day of the month with leading zeroes (01 - 31) 26
D Three character of the day Mon
j Day of the month without leading zeroes (1 - 31) 26
l The full name of the day of the week (Sunday - Saturday) Friday Monday
N Day of the week in numeric (Monday(1) - Sunday(7)) 1
S Suffix for day of the month (st, nd, rd, th) th
w Day of the week, numerical (Sunday (0) - Saturday (6)) 1
z Day of the year(0 - 365) 268

Example of day


echo "Today is:";
echo date ("l");



Today is:Monday

You can use all the above parameter according to your need. For example, if you want to print the starting three letters of the day use "D" as a argument.


Format Description Example
W week number of year 39

Week example


echo " number of Week in year:" .date ("W")."
"; ?>

Argument "W" is used for printing the numbers of week in the current year as shown in the above example.


Format Description Example
F Full name of the month (January - December) September
m Numeric representation of the month(01 - 12) 09
M Three letter of the month Sep
n Numeric representation of month without 0 9
t number of the day 30

Month example

echo "Full name of the month : - ";

echo date ("F")."
"; echo "Month in form of integer : - "; echo date ("m")."
"; echo "Three letter of the month : - "; echo date ("M")."
"; echo "Month in numbers: - "; echo date ("n")."
"; echo "Integer form of the day : - "; echo date ("t")."
"; ?>

The above example shows all the argument of the month for example, uses "F" as an argument for the full name of the month.


Format Description Example
L Whether it is 1 (leap year) or its 0 ( not leap year) 0
Y Four digit representation of the year(1999 or 2014) 2022
y Two digit representation of the year(14) 22
o ISO-8601 version of 'Y', affected by the week ('W') (2014) 2022

Year example


echo "leap year or not- ";

echo date ("L")."
"; echo "Four digit of year: - "; echo date ("Y")."
"; echo "Last two digit of the year: - "; echo date ("y")."
"; echo "ISO-8601 version of 'Y', affected by the week ('W') : - "; echo date ("o")."
"; ?>

In the above example, argument "L" checks whether current year is a leap year or not, if the current year is leap year return 1 otherwise return 0.


Format Description Example
a Lowercase am or pm am
A Uppercase AM or PM AM
B Swatch internet time (000 – 999) 150
g 12-hour format of the hour, without leading zeroes (1 – 12) 2
h 12-hour format of the hour, with leading zeroes (01 – 12) 02
G 24-hour format of the hour, without leading zeroes (0 – 23) 2
H 24-hour format of the hour, with leading zeroes (00 – 23) 02
i Minutes with leading zeroes(00 – 59) 37
s Seconds with leading zeroes(00 – 59) 06
u Milliseconds (e.g. 54321) 000000

Time example

echo "The time is " . date ("h:i:sa");


The above example shows the current time in 12-hour format, you can also print the current day in 24-hour format with the help of "G" and "H" argument.


Wicked men obey from fear, good men from love