PHP Date function With Examples on Formatting Dates

PHP Date() function

PHP date() is used to convert a timestamp to a format that is more human readable dates and times.

Syntax
The syntax of the PHP date() is as given below.

There are two parameters in the PHP date function. These two parameters are format and timestamp. Check the table to understand them.

Parameter Name Description
Format Format is the required parameter which can be used in the PHP date() to specify the format of the date and time.
timestamp timestamp is not the required parameter. You can use this parameter to specify the timestamp. If you do not use this parameter then the current date and time will be used to display.

PHP date format parameters

format Character Description Example
d It represents the day of the month which is of two digits. 01 to 31
D It represents the text format of the day of the week which is of three letters in size. Mon to Sun
m It represents the months in the numeric format. For the months in single digit, it starts with the leading zero. 01 to 12
M It represents the months in the text format which is of three digits. Jan to Dec
y It represents the year in the two digits number. 03 or 17
Y It represents the year of the four digits number. 2003 or 2017

Example1

The above example prints the date like 20/11/2017 format.

Example2

This will print the date in the format 20 Nov 2017.

How to get the current date in PHP?

Sometimes on your website, you want to print the current date. You don’t need to manually update the dates and times on your website, PHP date function will do it all for you. Use the below PHP code to print the current date.

Here, in the above code, you can change the position of the year, month and date according to your need. H:i:s is used here to print the current time with the current date.

Get Automatic Copyright year for your website footer

Every blogger and website owners want to provide an updated website to their readers. But sometimes and somewhere else they forget updated dates which can give your readers, bad impression of website.

Suppose, for example, you are putting copyright text on your website footer area and not using any solution to update it automatically. If you forget to update it manually, you are losing the trust of your readers by showing the wrong and old dates.

Here, I am giving you a solution which you can use on your website and update your date with the copyright text. Use below PHP code and change our website name with your website name.

You must also read:

Resource