Problem seems to be the dot after the abbreviated month
$format = 'php:d M Y';
$timestamp = 1549026000;
$date = Yii::$app->formatter->asDate($timestamp, $format);
echo $date.PHP_EOL; // 02 Feb. 2019
$validator = new \yii\validators\DateValidator();
$validator->format = $format;
echo $validator->validate($date) ? 'valid' : 'invalid'; // invalid
//let's remove the dot after the month and it works
$date = '02 Feb 2019';
echo $validator->validate($date) ? 'valid' : 'invalid'; // valid
Valid!
Invalid!
| Q | A
| ---------------- | ---
| Yii version | 2.0.12?
| PHP version | 7.1.23
| Operating system |OSX 10.14.2
$format = 'php:d M. Y';
$timestamp = 1549026000;
$validator = new \yii\validators\DateValidator();
$validator->format = $format;
$date = Yii::$app->formatter->asDate($timestamp, $format);
echo $date . " is " . ($validator->validate($date) ? 'valid' : 'invalid') . PHP_EOL;
$date = '02 Feb 2019';
echo $date . " is " . ($validator->validate($date) ? 'valid' : 'invalid') . PHP_EOL;
$date = '02 Feb. 2019';
echo $date . " is " . ($validator->validate($date) ? 'valid' : 'invalid') . PHP_EOL;
My output:
01 Feb. 2019 is valid
02 Feb 2019 is invalid
02 Feb. 2019 is valid
It seems like everything works well.
Do you have typo in the first line $format = 'php:d M Y';? Miss the dot?
Please try update to the last version of Yii. For me Yii::$app->formatter->asDate(1549026000, 'php:d M Y') returns 01 Feb 2019.
I tested on updated Yii (2.0.16) and the period is still being put in there. If I use the PHP function it doesn't include the period as expected.
echo Yii::$app->formatter->asDate(time(), 'php:M'); // Feb.
echo date('M'); // Feb
I do have the PHP Intl extension installed (v 1.1.0), but I don't see how this should matter.
I am using \yiii18n\Formatter in my config. Constructing directly gives same result of course
$formatter = new \yii\i18n\Formatter();
echo $formatter->asDate(time(), 'php:M'); // Feb.
Some locale versions (like Dutch, French) add period after short month name. This affects formatter with "php:" because of that line where format is being converted to ICU version to be able to use IntlDateFormatter.
Maybe it's time to stop doing this? I think it was @rob006 who already mentioned that if someone is asking for php format directly hidden switching to ICU is bad.
Related: https://github.com/yiisoft/yii2/issues/16539
/cc @cebe
Most helpful comment
Some locale versions (like Dutch, French) add period after short month name. This affects formatter with "php:" because of that line where format is being converted to ICU version to be able to use
IntlDateFormatter.Maybe it's time to stop doing this? I think it was @rob006 who already mentioned that if someone is asking for php format directly hidden switching to ICU is bad.