When registering a user, I want to suggest the choice of his time zone (time zone). But at the same time, the list of time zones should be shown in the user's native language (locale "ru_RU", or "en_US", or whatever). As far as I understand, it is necessary to climb in ICU to get the canonical names of the time zones. And then merge arrays from different locales. How to implement this?

  • Most likely you will have to manually localize .. - vp_arth
  • That is, independently translate all the names of time zones? But then when changing the library ICU will need to again interfere with the code. Here is one person pulling some data from the library by locale: http://intl.rmcreative.ru/site/zone-data?locale=ru_RU There is actually a lot of data in this library. You just need to be able to get them. The site has a search for any locale. - girmate
  • Hmm, some kind of tin: 3v4l.org/WK1rV - vp_arth
  • @vp_arth, thank you, somewhere close. But returns 0 => string 'Jan 1, 1970, 3:00:00 AM Moscow Standard Time' (length = 44) 1 => string 'Jan 1, 1970, 3:00 : 00 AM Moscow Standard Time '(length = 44) and so many times in a row. The $ tz variable had to be made null (in the second function), because it produces an error — the constructor is incorrect (PHP 7.0). How to fix it? And why is the data set in the array the same? - girmate
  • one
    Thanks, @vp_arth! On the basis of your when the problem was solved. - girmate

1 answer 1

It seems to work. Only 6 exceptions from 425 elements are caught.

$except = []; $timezonesData = []; $timezonesIdentifiers = \DateTimeZone::listIdentifiers(); foreach ($timezonesIdentifiers as $timezone) { try { $formatter = new \IntlDateFormatter( "en_US", \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $timezone, \IntlDateFormatter::GREGORIAN, 'zzzz - ZZZZ - vvvv - VV - VVV - VVVV' ); if ($formatter) { $t10n = $formatter->format(0); } else { $except[] = 'exception 1 '.$e->getMessage(); // continue; } } catch (\Throwable $e) { $except[] = 'exception 2 '.$e->getMessage(); // continue; } catch (\Exception $e) { $except[] = 'exception 3 '.$e->getMessage(); // continue; } $timezonesData[$timezone] = $t10n; } var_dump($except); var_dump($timezonesData); 

It should be noted that the PHP - intl extension with the ICU library (newer) should be installed. Further convert the current code and, I think, it will be easy to get the list in the form that is needed. Thanks to all.