How to Convert an Existing PHP DateTime Object From One Time Zone to Another?

If you have an already instantiated DateTime object, then you can change its time zone by calling the DateTime::setTimezone() method, for example, in the following way:

$existingDateTime->setTimezone(new DateTimeZone('Europe/Berlin'));

For example, consider the following DateTime object that's instantiated with a time zone set to 'Europe/London', but is later changed to 'Europe/Berlin' by using the DateTime::setTimezone() method:

$dt = new new DateTime('now', new DateTimeZone('Europe/London'));
// ...
$dt->setTimezone(new DateTimeZone('Europe/Berlin'));
// ...

You can get the list of supported time zones from the official PHP documentation.


This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.