Yii2: Datetime placeholder formatting not working as expected

Created on 2 Oct 2014  路  2Comments  路  Source: yiisoft/yii2

I found bug with date&time formatting.
I set timezone to UTC (php.ini, yii config) and locale to en_US in my application.
When I use time placeholder in translations I always get wrong result. But when I use formatter class I get what I expected.

I debugged Yii code and found that translations is using MessageFormatter class, but Formatter->asTime is using IntlDateFormatter.

And looks like MessageFormatter class does not takes in account timezone.

Code:

var_dump(date("g:i A"));
var_dump(\Yii::$app->formatter->asTime(time(), 'short'));
var_dump(\Yii::t('time', '{0, time, short}', time()));

Result:

string '3:17 PM' (length=7)
string '3:17 PM' (length=7)
string '6:17 PM' (length=7)

I know it's not a Yii bug, but maybe this bug can be resolved on framework level.

Versions:
PHP: 5.4.16
ICU: 49.1.2

Issue related to PHP 5.2:
https://bugs.php.net/bug.php?id=58756

hard bug

Most helpful comment

@cebe @samdark

It seems that this is no longer relevant. Code:

echo PHP_VERSION . "\n";
echo INTL_ICU_DATA_VERSION . "\n";
echo INTL_ICU_VERSION . "\n";

date_default_timezone_set('America/Denver');
echo date("g:i A") . "\n";
echo \Yii::$app->i18n->format('{0, time, short}', time(), 'en_US') . "\n";

date_default_timezone_set('Europe/Moscow');
echo date("g:i A") . "\n";
echo \Yii::$app->i18n->format('{0, time, short}', time(), 'en_US') . "\n";

Result:

7.1.15-1+ubuntu16.04.1+deb.sury.org+2
55.1
55.1
7:16 AM
7:16 AM
4:16 PM
4:16 PM

The intl MessageFormatter still has no option to configure timezone: http://bugs.icu-project.org/trac/ticket/9330

But as you can see from my example it uses default_timezone.

All 2 comments

related to #4960 do not have an idea for a solution right now. Afaik the intl MessageFormatter has no option to configure the builtin number and date formatters.

@cebe @samdark

It seems that this is no longer relevant. Code:

echo PHP_VERSION . "\n";
echo INTL_ICU_DATA_VERSION . "\n";
echo INTL_ICU_VERSION . "\n";

date_default_timezone_set('America/Denver');
echo date("g:i A") . "\n";
echo \Yii::$app->i18n->format('{0, time, short}', time(), 'en_US') . "\n";

date_default_timezone_set('Europe/Moscow');
echo date("g:i A") . "\n";
echo \Yii::$app->i18n->format('{0, time, short}', time(), 'en_US') . "\n";

Result:

7.1.15-1+ubuntu16.04.1+deb.sury.org+2
55.1
55.1
7:16 AM
7:16 AM
4:16 PM
4:16 PM

The intl MessageFormatter still has no option to configure timezone: http://bugs.icu-project.org/trac/ticket/9330

But as you can see from my example it uses default_timezone.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SamMousa picture SamMousa  路  3Comments

chaintng picture chaintng  路  3Comments

MUTOgen picture MUTOgen  路  3Comments

Locustv2 picture Locustv2  路  3Comments

sahifedp picture sahifedp  路  3Comments