How can one handle different time zones when working with timestamps in PHP?

When working with timestamps in PHP, it's important to consider different time zones to ensure accurate date and time calculations. One way to handle this is by setting the default time zone using the date_default_timezone_set() function to the desired time zone before working with timestamps. This will ensure that all date and time functions in PHP use the specified time zone.

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

// Get the current timestamp
$currentTimestamp = time();

// Format the timestamp in the desired time zone
$formattedDate = date('Y-m-d H:i:s', $currentTimestamp);

echo $formattedDate;