How to change date format in PHP?

By Hardik Savani | October 2, 2019 | | 1019 Viewer | 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...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: