How can PHP developers accurately format time values in different time zones using date() and gmdate() functions?

When working with time values in different time zones, PHP developers can accurately format these values by using the date() function for local time and the gmdate() function for GMT/UTC time. By converting the time values to the desired time zone before formatting, developers can ensure that the displayed time reflects the correct time zone.

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

// Get the current time in local time zone
$local_time = time();

// Convert the local time to GMT/UTC time
$gmt_time = gmdate('Y-m-d H:i:s', $local_time);

// Format the local time and GMT/UTC time
$formatted_local_time = date('Y-m-d H:i:s', $local_time);
$formatted_gmt_time = $gmt_time;

echo "Local Time: " . $formatted_local_time . "<br>";
echo "GMT/UTC Time: " . $formatted_gmt_time;