In this post, I would like to tell you about “How to change date format DD/MM/YYYY to YYYY-MM-DD in PHP”. I will convert date format using simple date() and simple strtotime() functions of php.
I will give you very quick and simple example that helps you a-lot to simply change date formats in php. It not only for DD/MM/YYYY to YYYY-MM-DD. You can also convert them in other formats also. I will show just few of them.
Mostly want to change date formats when we need to insert or retrieve date in database. So I will give you example that how it could be done.
So, let’s see the example:
1- Converting yyyy-mm-dd to mm/dd/yyyy
In this example I will add “2020-03-29” with (YYYY-MM-DD) format and I will change it into MM/DD/YYYY format using PHP.
<?php $currentDate = "2020-03-29"; $convertedDate = date("m/d/Y", strtotime($currentDate)); print_r($convertedDate); ?>
Output:
03/29/2020
2- Convert yyyy-mm-dd to dd-mm-yyyy
In this example I will add “2020-03-29” with (YYYY-MM-DD) format and I will change it into DD-MM-YYYY format using PHP.
<?php $currentDate = "2020-03-29"; $convertedDate = date("d-m-Y", strtotime($currentDate)); print_r($convertedDate); ?>
Output:
29-03-2020
3- Convert dd/mm/yyyy to yyyy-mm-dd
In this example, I will add “29/03/2020” with (DD/MM/YYYY) format and I will change it into YYYY-MM-DD format using php.
<?php $currentDate = "29/03/2020"; $convertedDate = str_replace('/', '-', $currentDate ); $myNewDate = date("Y-m-d", strtotime($convertedDate)); print_r($myNewDate); ?>
Output:
2020-03-29
I hope it will definitely helps you to figure out “How to change date format DD/MM/YYYY to YYYY-MM-DD in PHP”. So, If you feel it is useful then don’t forget to share it with others by clicking on share button