I’m currently using this to convert a timestamp to a user’s defined timezone. My problem is that DateTimeZone() requires a timezone like Europe/Vienna or America/Chicago:
$date = new DateTime("@".$timestamp);
$date->setTimezone(new DateTimeZone("America/Chicago"));
I already looked into supported timezones on https://www.php.net/manual/en/timezones.php but there are so many of them and I don’t want users to browse the whole list.
Is there a simple way converting e.g. GMT+1:00, GMT-4:30 or GMT+5:45 to a correct value for DateTimeZone().
Or is it better to use an array list like I found here: Convert selected time and timezone to a set timezone
Is it better to use UTC or GMT in general for the user to pick?
Thanks!
You should probably obtain the user’s real timezone (selected from the complete list). Using only a static offset from UTC you will not be able to follow the correct daylight savings time rules for the user’s location.
PHP’s
DateTimeZonedoesn’t appear to accept POSIX timezone strings so it looks like you’re stuck with predefined timezones.Look in
/usr/share/zoneinfo/Etc. There are a bunch of timezones predefined there for UTC plus and minus an integer. The only gotcha is that the sense of + and – is reversed from the normal convention. So just use, for example, “Etc/GMT-9” for a generic timezone with offset +0900.This doesn’t handle all of the possible timezones, like Nepal’s weird +0545 but it looks like it’s the best option that’s easily accessible.