How to Convert or Change Date Format to Another in PHP

Last Updated on December 27, 2020 by Roshan Parihar

In this tutorial, learn how to change date format using PHP. The short answer is to use the strtotime() and date() functions to change date format in PHP.

To convert the date format, the strtotime() function first converts the input date and time into seconds. After that, the date() function converts the seconds into a specified date format. See the examples given below on how to change date formate in PHP

How to Change Date Format Using strtotime() and date() in PHP

You can change the date format using strtotime() and date() functions in PHP. You can find the various example below to use the function for date conversion.

If you have a date in format YYYY-MM-DD as ‘2020-06-17’ and you want to change it to date format DD-MM-YYYY. You can use the example given below:-

Change Date Format YYYY-MM-DD to DD-MM-YYYY

Output

The new date in DD-MM-YYYY Format is: 17-06-2020

For changing the date in format YYYY-MM-DD as ‘2017-08-23’ to date format DD-MM-YYYY, use the example given below:-

Get Convert YYYY-MM-DD to MM-DD-YYYY

Output

The new date in MM-DD-YYYY Format is: 23-08-2017

You can also get the required type and add dots(.) and slashes(/) to the dates. See the example below:-

Format YYYY-MM-DD to DD.MM.YYYY or DD/MM/YYYY

Output

The new date with a dot in DD.MM.YYYY Format is: 17.06.2020
The new date with a slash in DD/MM/YYYY Format is: 17/06/2020

Convert Date Format Using DateTime() Function in PHP

In addition to the above method, you can also use the DateTime() function to convert the date format in PHP.

If you have a date in DD-MM-YYYY format as ’26-04-2020′, you can convert this to date format YYYY-MM-DD using the example given below:-

Convert Date DD-MM-YYYY to YYYY-MM-DD

Output

The new date in YYYY-MM-DD Format is: 2020-04-26

The DateTime() function can also be used to convert dates with dots(.) and slashes(/). See the example given below:-

Get DD-MM-YYYY to YYYY.MM.DD or YYYY/MM/DD

Output

The new date with a dot in YYYY.MM.DD Format is: 2020.04.26
The new date with a slash in YYYY/MM/DD Format is: 2020/04/26

You can use both the above methods to convert the date format in PHP. However, the second method using DateTime() function can be more useful for you to convert any type of date to the required type.

You May Also Like to Read