How can PHP be used to convert dates from a "dd.mm.yyyy" format to a "yyyy-mm-dd" format?
To convert dates from a "dd.mm.yyyy" format to a "yyyy-mm-dd" format in PHP, you can use the `DateTime` class. First, you need to create a `DateTime` object from the input date string using `createFromFormat()`, and then format the date using the `format()` method with the desired format.
$inputDate = '25.12.2021';
$dateObj = DateTime::createFromFormat('d.m.Y', $inputDate);
$outputDate = $dateObj->format('Y-m-d');
echo $outputDate; // Output: 2021-12-25
Keywords
Related Questions
- What are best practices for including external libraries like PEAR in PHP scripts?
- What are the best practices for optimizing search queries in PHP when dealing with multiple database tables?
- In the context of creating an RSS reader in PHP, what are the advantages of using libraries like SimplePie compared to manually parsing XML feeds?