What are the best practices for converting time formats in PHP to ensure accurate calculations?

When converting time formats in PHP, it is important to ensure that the time zone is set correctly to avoid discrepancies in calculations. One best practice is to use the DateTime class along with the DateTimeZone class to accurately convert and manipulate time formats.

// Set the default time zone
date_default_timezone_set('UTC');

// Create a DateTime object with the input time and format
$inputTime = DateTime::createFromFormat('Y-m-d H:i:s', '2022-01-01 12:00:00', new DateTimeZone('UTC'));

// Convert the input time to a different time zone
$inputTime->setTimezone(new DateTimeZone('America/New_York'));

// Format the converted time
$convertedTime = $inputTime->format('Y-m-d H:i:s');

echo $convertedTime;