How can PHP code be written to handle date conversions correctly to avoid unexpected results like the date "01.01.1970"?

When handling date conversions in PHP, it is important to ensure that the input format of the date is specified correctly to avoid unexpected results like displaying "01.01.1970" which is the Unix epoch start date. To solve this issue, you can use the `DateTime` class in PHP along with `createFromFormat()` method to explicitly define the input date format and then convert it to the desired output format.

$input_date = "15-03-2022"; // Input date in the format dd-mm-yyyy
$input_format = "d-m-Y"; // Specify the input date format
$output_format = "Y-m-d"; // Specify the desired output date format

$date = DateTime::createFromFormat($input_format, $input_date);
$output_date = $date->format($output_format);

echo $output_date; // Output: 2022-03-15