php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Intl Extension: MessageFormatter prepends date with space

Open frederikbosch opened this issue 4 years ago • 1 comments

Description

The following code:

<?php

$formatter = new \MessageFormatter('en_US', '{0, date, YYYY-ww}');
var_dump($formatter->format([new \DateTimeImmutable]));

$formatter = new \MessageFormatter('en_US', '{0}');
var_dump($formatter->format(['test']));

Resulted in this output:

string(8) " 2021-51"
string(4) "test"

But I expected this output instead:

string(8) "2021-51"
string(4) "test"

See this 3v4l online page. This applies to all messages using date.

PHP Version

7.3.13 - 7.3.33, 7.4.0 - 7.4.27, 8.0.0 - 8.0.13, 8.1.0

Operating System

Any

frederikbosch avatar Dec 15 '21 09:12 frederikbosch

Unfortunately the ICU documentation doesn't go into these kinds of specifics, but given that PHP is not the one deciding to insert the extra space, I'm going to assume that it's deliberate behavior by the libicu library to respect the leading whitespace used by your custom date style. Change it to '{0,date,YYYY-ww}' to match how they write their formatting patterns. https://3v4l.org/OlhBU

damianwadley avatar Dec 15 '21 09:12 damianwadley