How to Change Date Format in PHP?

By Hardik Savani November 5, 2023 Category : 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 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

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

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

$myOriginalDate = "15/08/2019";

$myOriginalDate = str_replace('/', '-', $myOriginalDate );

$myNewDate = date("Y-m-d", strtotime($myOriginalDate));

print_r($myNewDate);

Output:

2019-08-15

Now you can check anyone, it will help you.

I hope it can help you...

Shares