What are the best practices for handling time zone conversions in PHP applications?

Handling time zone conversions in PHP applications is crucial for accurately displaying and manipulating dates and times across different time zones. The best practice is to always store date and time values in a consistent time zone (such as UTC) and convert them to the desired time zone for display purposes. This helps avoid confusion and ensures that dates and times are displayed correctly to users in different regions.

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

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

// Convert the DateTime object to a specific time zone (e.g. 'America/New_York')
$now->setTimezone(new DateTimeZone('America/New_York'));

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