Skip to content
Advertisement

Parse a UTC date, output local datetime

I have already read How to get local time in php? and similar questions, but here I want to do the contrary:

With a given datetime in UTC (e.g. 2021-03-31 23:45:00), how to output the date in local timezone?

JavaScript

In Europe/Paris timezone, it should output 2021-04-01 01:45:00, but here it sill outputs 2021-03-31 23:45:00. How to fix this?

I also tried with strtotime with a similar result; I also tried with:

JavaScript

without success.

Advertisement

Answer

You need to change the timezone of the date (using DateTime::setTimeZone()), not the default timezone:

JavaScript

Output:

04/01/2021, 01:46:14

Changing the default timezone affects the new DateTime(), not the format() result.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement