I’ve been reading about locales in PHP and it seems setlocale() has problems with threads. (I’m not too familiar with threads – the docs mention it is not thread safe)
I’d like to give my project the ability to deal with certain number formats and the Intl extension seems interesting.
http://php.net/manual/en/book.intl.php
Should I expect the same problems that setlocale() has using the Intl extension?
Ok, I was curious about this myself as well, so I devised a test.
First I tested
setlocale()with these two files:and
Then I executed them in two seperate tabs. First
locale1.php, that sleeps for 10 seconds after setting the locale, giving us time to executelocale2.phpin the meanwhile.To my surpriselocale2.phpisn’t even allowed to change the locale correctly. It appearssleep( 10 )inlocale1.phphijacks the Apache/PHP process in such a way that it doesn’t allowlocale2.phpto alter the locale in the meanwhile. It does however echo the date in the meanwhile of course, just not localized as you’d expect.Edit: sorry, scrap that. It appears
locale2.phpdoes change the locale andlocale1.phpthen prints the English date in stead of Dutch after sleeping. So that does appear to be in accordance with what is expected behavior fromsetlocale()./Edit
Then, I tested
IntlDateFormatterwith these two files:and
and then executed them again in two separate tabs, the same way as with the first set of files. This does give the expected results: while
locale1.phpis sleepinglocale2.phpnicely prints a date in American-English according to American rules, after whichlocale1.phpnicely prints a date in Dutch according to Dutch rules.So, concluding, it appears
Intlis safe from thatsetlocaleproblem.But also mind Hyunmin Kim’s answer of course. I couldn’t comment on that, due to lack of experience with using
Intl. I only recently discoveredIntl.