How to set the timezone for a DateTime object in PHP?

To set the timezone for a DateTime object in PHP, you can use the DateTime::setTimezone() method. Here's an example:

$datetime = new DateTime('2022-07-01 12:00:00', new DateTimeZone('UTC')); $datetime->setTimezone(new DateTimeZone('America/New_York')); // Set the timezone to New York echo $datetime->format('Y-m-d H:i:s'); // Output: 2022-07-01 08:00:00

In the example above, we first create a DateTime object with the initial timezone as UTC. Then, we use the setTimezone() method to change the timezone to America/New_York. Finally, we format and output the updated datetime value.

Make sure to specify the appropriate timezone string for the desired timezone. You can find a list of all supported timezones in PHP documentation (https://www.php.net/manual/en/timezones.php).