DateTime::format() or strftime() and Yii::app()->language

I have a strange issue with results returned by DateTime::format() or strftime(), that seems to be ignoring Yii::app()->language settings.

I have a dual-language (English and Polish) application written in Yii 1.x. No matter, what language is currently set in Yii::app()-language setting, results returned by PHP’s DateTime::format() or date() are always in English. For example DateTime::format('j F Y') should print month name in Polish, when current app’s language is set to pl (I use short notation, if that matters).

At first, I thought, that setting Yii::app()-language does not control date() or DateTime::format (is it?). But, then it turned out, that I’m unable to force PHP to write month name in Polish even after manually setting setlocale(LC_ALL, 'pl_PL');.

After some research, I found out, that DateTime::format() does not pay attention to locales and I should use strftime instead. However, either I’m doing something wrong does not work at all in my local, Windows-based environment.

For first test, I took following code:


setlocale(LC_TIME, "pl_PL");

echo '$startDate->getTimestamp() = '.$startDate->getTimestamp();

echo 'strftime("%a %e.%l.%Y", $startDate->getTimestamp()) = '.strftime("%a %e.%l.%Y", $startDate->getTimestamp());



And the results are as following:

6406

pucha_1.png

So, either I’m missing something or after feeding strftime with correct timestamp, it prints… nothing.

Then, I used [an example code from strftime's documentation. And, again, no matter, what language is selected by user, it always prints texts in English. Here is an example:

6407

pucha_2.png

What am I missing? I have absolutely no idea, why strftime does not work on my system at all (prints nothing or always English text) and why it is ignoring Yii::app()->language?

Can someone shed some light on this problem? How to fix it or what other method use to finally get dates printed with locale matching Yii::app()->language setting?

Try two things for Windows:

  1. Set LC_ALL instead of LC_TIME

  2. Set ‘POL’ instead of ‘pl_PL’




Yii::app()->dateFormatter->formatDateTime($timestamp, 'full', 'full');



CDateFormatter

Pure PHP-based solution, proposed by Bizley did not work, even though I tested all possible combinations with range of locales (first parameter) and language code (second parameter).

Yii-based solution, proposed by phtamas, works like a charm.

This makes this case even more suspicious to me. Before testing, I was more than sure, that it is something with my local edition of Apache and that neither method will work. The fact that Yii approach works wile pure PHP don’t surprises me. How can one method work and other one not?

Aye, I dig up a bit about this and looks like good old Windows has got some problems with giving the proper localisation values.

Anyway, it’s better to rely on Yii since all the i18n data is stored in /framework/i18n/data and it doesn’t depend on environment.