Home >Backend Development >PHP Tutorial >How to convert date format in PHP
This article uses PHP strtotime() and date() functions to convert date and time format. For example, if a date is stored in a variable in YYYY-MM-DD format, it needs to be changed to MM-DD-YYYY format.
We can achieve this by first converting the date to seconds using the strtotime() function. After using the date() function to reconstruct the date to any format. Here are a few conversion examples:
1. Change YYYY-MM-DD => MM-DD-YYYY
Here we have the date yyyy-mm-dd ("2019-02-18") format and convert it to mm-dd-yyyy ("02-18-2019") format.
$origDate = "2019-02-18"; $newDate = date("m-d-Y", strtotime($origDate)); echo $newDate;
The output is:
02-18-2019
2. Change YYYY-MM-DD => DD-MM-YYYY
Here we have the date yyyy-mm-dd ("2019-02-18") format and convert it to dd-mm-yyyy ("18-02-2019") format.
$origDate = "2019-02-18"; $newDate = date("d-m-Y", strtotime($origDate)); echo $newDate;
The output result is:
18-02-2019
This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !
The above is the detailed content of How to convert date format in PHP. For more information, please follow other related articles on the PHP Chinese website!