This Article helps to you date format in php using strtotime() as well as date() methods to convert date time formats for user friendly. For simple example you have stored a date like as a YYYY-MM-DD default formats in a php variable as well as required to update this to MM-DD-YYYY date formats.
How do I Convert Date Format in PHP
I can achive this by converting date first to seconds using strtotime() methods. After that reconstruct date to any formats using date() methods. following is some more examples of conversion:
1. Change YYYY-MM-DD => MM-DD-YYYY
Here i have date yyyy-mm-dd (“2021-04-25”) formats as well as new fresh converting it to mm-dd-yyyy (“04-25-2021”) date formats.
$startDt = "2021-04-25"; $resultDt = date("m-d-Y", strtotime($startDt)); echo $resultDt;
- How to convert date formats in PHP?
- Simple PHP Convert Date Formats Examples
- PHP Date and Time Formatting Functions
2. Change YYYY-MM-DD => DD-MM-YYYY
and then i have date yyyy-mm-dd (“2021-04-25”) date formats as well as converting it to dd-mm-yyyy (“25-04-2021”) date formats.
$startDt = "2021-04-25"; $resultDt = date("d-m-Y", strtotime($startDt)); echo $resultDt;
3. Change DD/MM/YYYY => YYYY-MM-DD
If you have slashes in date formats such as a “25/04/2021” and required to convert / with hyphens (-). The bellow simple example will help you to convert DD/MM/YYYY (“25/04/2021”) to YYYY-MM-DD (2021-04-25).
$startDt = "25/04/2021"; $date = str_replace('/', '-', $startDt ); $resultDt = date("Y-m-d", strtotime($date)); echo $resultDt;
I hope you get an idea about date format in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.