How to Change Date Format in PHP?

In this post, i would like to share with you how do i change date format DD/MM/YYYY to YYYY-MM-DD, MM-DD-YYYY to YYYY-MM-DD, d/m/Y to Y-m-d in php. we will convert date format using date() and strtotime() of php.

I will give you very simple example that helps you to simply change date formate in php. we most of need to change date format when you store date into database and when you display on list page. so i will give you bellow three example that will help you to easily convert date format in php.

So, let's see three example, so you can use in your core php application.Convert yyyy-mm-dd to mm/dd/yyyy in PH

In this example we will add "2019-08-15" with (YYYY-MM-DD) format and we will change into MM/DD/YYYY format in php.

 

Convert yyyy-mm-dd to mm/dd/yyyy in PHP

In this example we will add "2019-08-15" with (YYYY-MM-DD) format and we will change into MM/DD/YYYY format in php.

$myOriginalDate = "2019-08-15";
$myNewDate = date("m/d/Y", strtotime($myOriginalDate));
print_r($myNewDate);

 

OUTPUT
08/15/2019

 

Convert yyyy-mm-dd to dd-mm-yyyy in PHP

In this example we will add "2019-08-15" with (YYYY-MM-DD) format and we will change into DD-MM-YYYY format in php.

$myOriginalDate = "2019-08-15";
$myNewDate = date("d-m-Y", strtotime($myOriginalDate));
print_r($myNewDate);

 

OUTPUT
15-08-2019

 

Hope it can help you.