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;
Output:
04-25-2021
Also Read:
- 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;
Output:
25-04-2021
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;
Output:
2021-04-25
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.