PHP Date and Time

Sometimes we may need to display current date and time in our program and that is why we use the date() and time() functions to display the date and time.

The date() function in PHP

if you want to display date and time in a more readable language then we need to use the date() and time() functions with timestamp.

Syntax of date() function in PHP

Format: The format parameter is required in date() function. So it is must to define this parameter to display the current date.

Timestamp: It is not compulsory to pass the timestamp variable in the date() function in PHP. And the default value of the timestamp will the current date and time. So if we don’t pass this parameter this it will by default display the current date in the format as defined in the date() function.

How to display the current date in PHP

To display the current date, we will use the date() function as shown in the following example:

How to display the current time in PHP

You can display the current time as shown in the following example:

There are so many parameters which can be used to display the current date and time in different format. Here is the list of parameters in PHP:

The list of characters used as parameter in date() function.
character Details Example
Day characters
d displays the day of month leading with zeros 01 to 31
D This will display the textual representation of a day with three letters Mon        to         Sun
j Displays the day of the month without leading zeros 1 to 31
l (lowercase ‘L’) full representation of the week days Sunday

to

Saturday

N numeric representation of the day of the week like 1 (for Monday) 7 (for Sunday)
S suffix for the day of the month st, nd, rd or th. Works well with j
w Its a Numeric representation of the day of the week like 0 (for Sunday) and 6 (for Saturday)
z Its used for the day of the year 0 to 365
Week
W week number of year like 42, the 42nd week of the year
Month
F A full representation of a month January  to December
m Numeric representation of the month with zeros 01 – 12
M A textual representation of a month with three characters only. Jan – Dec
n a numeric representation of the month without zeros 1 – 12
t used to display the number of days in the month 28 – 31
Year
L Check if it’s a leap year 1 if it is a leap year else  0 .
o This is same as Y, except if the ISO week number (W) belongs to the previous or next year Examples: 1999 or 2003
Y displays the current year with 4 digits. Like: 2017
y two digit representation of year Like: 16 or 17
Time
a Lowercase am or pm am or pm
A Uppercase AM or PM AM or PM
B Swatch Internet time 000 – 999
g displays 12-hour time format of an hour without zeros 1 – 12
G displays 24-hour time format of hour without zeros 0 – 23
h displays 12-hour format of hour with leading zeros 01 – 12
H displays a 24-hour format of hour with leading zeros 00 – 23
i Minutes with zeros 00 – 59
s Seconds leading zeros 00 – 59

 

Leave a Reply

Your email address will not be published.