What are some common methods in PHP to convert a date from DD.MM.YYYY format to YYYY-MM-DD format?
When converting a date from DD.MM.YYYY format to YYYY-MM-DD format in PHP, one common method is to use the `DateTime` class to parse the input date and then format it in the desired output format using the `format()` method. Another approach is to use the `date_create_from_format()` function to create a `DateTime` object from the input date format and then use the `format()` method to output it in the desired format.
```php
$inputDate = '25.12.2021';
$dateObj = DateTime::createFromFormat('d.m.Y', $inputDate);
$outputDate = $dateObj->format('Y-m-d');
echo $outputDate;
```
This code snippet takes an input date in DD.MM.YYYY format, creates a `DateTime` object using `createFromFormat()`, and then formats the date in the YYYY-MM-DD format using the `format()` method. The output will be '2021-12-25' for the input date '25.12.2021'.
Related Questions
- In PHP, how can developers ensure efficient and effective search functionality when users can select multiple options for filtering data?
- How can the glob() function be used to create a whitelist of HTML files for validation in PHP?
- Why is it important to avoid using reserved words like "STATUS" in MySQL queries?