Skip to content

Commit

Permalink
added example for using unsupported PHP format in yii formatter
Browse files Browse the repository at this point in the history
fixes #14278
  • Loading branch information
cebe committed Jun 9, 2017
1 parent fdc91f3 commit 2b9f000
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/guide/output-formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ echo Yii::$app->formatter->asDate('now', 'yyyy-MM-dd'); // 2014-10-06
echo Yii::$app->formatter->asDate('now', 'php:Y-m-d'); // 2014-10-06
```

> Info: Some letters of the PHP format syntax are not supported by ICU and thus the PHP intl extension and can not be used
> in Yii formatter. Most of these (`w`, `t`, `L`, `B`, `u`, `I`, `Z`) are not really useful for formatting dates but rather
> used when doing date math. `S` and `U` however may be useful. Their behavior can be achived by doing the following:
>
> - for `S`, which is the English ordinal suffix for the day of the month (e.g. st, nd, rd or th.), the following replacement can be used:
>
> ```php
> $f = Yii::$app->formatter;
> $d = $f->asOrdinal($f->asDate('2017-05-15', 'php:j'));
> echo "On the $d day of the month."; // prints "On the 15th day of the month."
> ```
>
> - for `U`, the Unix Epoch, you can use the [[yii\i18n\Formatter::asTimestamp()|timestamp]] format.
When working with applications that need to support multiple languages, you often need to specify different date
and time formats for different locales. To simplify this task, you may use format shortcuts (e.g. `long`, `short`), instead.
The formatter will turn a format shortcut into an appropriate format according to the currently active [[yii\i18n\Formatter::locale|locale]].
Expand Down
14 changes: 14 additions & 0 deletions tests/framework/i18n/FormatterNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,20 @@ public function testIntlAsOrdinal()
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asOrdinal(null));
}

/**
* https://github.com/yiisoft/yii2/issues/14278
*/
public function testIntlAsOrdinalDate()
{
$f = $this->formatter;
$this->assertSame('15th', $f->asOrdinal($f->asDate('2017-05-15', 'php:j')));
$this->assertSame('1st', $f->asOrdinal($f->asDate('2017-05-01', 'php:j')));

$f->locale = 'de_DE';
$this->assertSame('15.', $f->asOrdinal($f->asDate('2017-05-15', 'php:j')));
$this->assertSame('1.', $f->asOrdinal($f->asDate('2017-05-01', 'php:j')));
}

public function testIntlAsShortSize()
{
$this->formatter->numberFormatterOptions = [
Expand Down

0 comments on commit 2b9f000

Please sign in to comment.