How can PHP's DateTime class be utilized to handle time zone conversions more effectively?

When working with time zones in PHP, it is important to use the DateTime class to easily handle conversions between different time zones. By setting the time zone for the DateTime object and utilizing the DateTimeZone class, you can ensure that your date and time calculations are accurate and consistent across different time zones.

// Set the default time zone
date_default_timezone_set('America/New_York');

// Create a new DateTime object with the current time
$now = new DateTime();

// Set the desired time zone for conversion
$desiredTimeZone = new DateTimeZone('Asia/Tokyo');

// Convert the time to the desired time zone
$now->setTimezone($desiredTimeZone);

// Output the converted time
echo $now->format('Y-m-d H:i:s');