Can anyone suggest an easy method to convert date and time to different timezones in php?
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.