English spoken conference

New in Symfony 4.3: Timezone improvements

Symfony 4.3 will add a new Timezone validator to check that the given value is a valid timezone ID as defined by PHP. In addition to that validator, we've worked hard on many other features to improve the support of timezones.

Added timezones to the Intl component

The Intl component added a new Timezone class to get information about timezones, such as their names (in all languages):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
use Symfony\Component\Intl\Timezones;

\Locale::setDefault('en');

$timezones = Timezones::getNames();
// ('timezoneID' => 'timezoneValue')
// => ['America/Eirunepe' => 'Acre Time (Eirunepe)', 'America/Rio_Branco' => 'Acre Time (Rio Branco)', ...]

$timezones = Timezones::getNames('de');
// => ['America/Eirunepe' => 'Acre-Zeit (Eirunepe)', 'America/Rio_Branco' => 'Acre-Zeit (Rio Branco)', ...]

$timezone = Timezones::getName('Africa/Nairobi');
// => 'East Africa Time (Nairobi)'

$timezone = Timezones::getName('Africa/Nairobi', 'de');
// => 'Ostafrikanische Zeit (Nairobi)'

You can also check if a given timezone ID is valid:

1
$isValidTimezone = Timezones::exists($timezoneId);

Finally, you can get the timezone offset for any given timezone:

1
2
3
4
5
6
7
$offset = Timezones::getRawOffset('Etc/UTC');              // $offset = 0
$offset = Timezones::getRawOffset('America/Buenos_Aires'); // $offset = -10800
$offset = Timezones::getRawOffset('Asia/Katmandu');        // $offset = 20700

$offset = Timezones::getGmtOffset('Etc/UTC');              // $offset = 'GMT+00:00'
$offset = Timezones::getGmtOffset('America/Buenos_Aires'); // $offset = 'GMT-03:00'
$offset = Timezones::getGmtOffset('Asia/Katmandu');        // $offset = 'GMT+05:45'

Intl timezones in TimezoneType

The TimezoneType form element supports both strings and PHP timezones in its input option (which defines the format the timezone is stored on your underlying object). In Symfony 4.3 this option supports a new value called intltimezone to use \IntlTimeZone objects to store timezones.

Allow Intl timezones in validator

The Timezone validator added in Symfony 4.3 has been improved to also consider valid the ICU timezones, not only the PHP timezones. Besides, the list of ICU timezones has been updated to its 64.2 version. The only difference is that expired timezones cannot be used with IntlTimeZone.

In practice, this considers valid both UTC (the PHP format) and Etc/UTC (the ICU format), whereas Etc/UTC was considered wrong before (which is not).

Timezone names translation

This long-requested feature will finally make it in Symfony 4.3: the list of timezone names displayed by TimezoneType can be translated. Thanks to the new choice_translation_locale option, you can set the locale used to translate the timezone names before displaying them to the user:

1
2
3
4
5
6
7
8
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
// ...

$builder->add('timezone', TimezoneType::class, [
    // ...
    'choice_translation_locale' => 'uk',
    'intl' => true,
]);

In the example above, the timezones will be displayed in Ukrainian (locale = uk) instead of the default English, so the user will see things like за центральноєвропейським часом (Амстердам) instead of Central European Time (Amsterdam).

Comments

Great job, Roland, thank you!
Timezones::getRawOffset() is just wrong. You cannot get time zone offset without asking when do you want to know the offset. Many time zones change their offset during the year. Moreover, sometimes a time zone offset changes significantly, e.g. from -12 to +12 (or something like that). See https://www.youtube.com/watch?v=-5wpm-gesOY
Awesome
@Josef Kufner ... the method signature goes `getRawOffset(string $timezone, int $timestamp = time()): int` ;)

That's the current offset during the year, or for the given timestamp. Did i miss something?
Thanks for these useful additions!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.