How does the DateTime class in PHP handle date conversions from DD.MM.YYYY format to YYYY-MM-DD format?
When converting a date from DD.MM.YYYY format to YYYY-MM-DD format using the DateTime class in PHP, you can use the DateTime::createFromFormat() method to specify the input format and then use the format() method to output the date in the desired format.
$date = "31.12.2021";
$dateTime = DateTime::createFromFormat('d.m.Y', $date);
$newDate = $dateTime->format('Y-m-d');
echo $newDate; // Outputs: 2021-12-31
Related Questions
- In what ways does the implementation of a sandbox feature in a template engine contribute to the overall security of a PHP application?
- What are some best practices for ensuring that a PHP script does not output duplicate values when processing an array?
- How can PHP developers handle text line breaks after using openssl_decrypt?