How can PHP be used to format time based on different time zones?

To format time based on different time zones in PHP, you can use the DateTime class along with DateTimeZone class. You can create a DateTime object with the desired time and then set the timezone using DateTimeZone. Finally, you can format the time using the format() method.

// Set the default timezone
date_default_timezone_set('UTC');

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

// Set the desired timezone
$date->setTimezone(new DateTimeZone('America/New_York'));

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