Timezone conversion in php

后端 未结 9 1699
我寻月下人不归
我寻月下人不归 2020-11-22 04:19

Can anyone suggest an easy method to convert date and time to different timezones in php?

9条回答
  •  -上瘾入骨i
    2020-11-22 04:38

    I always struggle to remember how exactly setTimezone() method works. Is it adjusting datetime according to timezone? Or does it take a given datetime, drops its timezone, and uses the one you pass? All in all, I'd rather have a more intuitive way to work with timezones.

    I like this one:

    (new AdjustedAccordingToTimeZone(
        new DateTimeFromISO8601String('2018-04-25 15:08:01+03:00'),
        new Novosibirsk()
    ))
        ->value();
    

    It outputs a datetime in ISO8601 format, in Novosibirsk timezone.

    This approach uses meringue library, check out a quick start for more examples.

提交回复
热议问题