好吧,我自己也对此感到好奇,所以我设计了一个测试。
首先我测试了setlocale()
与这两个文件:
<?php
# locale1.php
error_reporting( E_ALL | E_STRICT );
date_default_timezone_set( 'Europe/Amsterdam' );
setlocale( LC_ALL, 'dutch_nld' ); // awkward Windows locale string
sleep( 10 ); // let's sleep for a bit here
echo strftime( '%A, %B %d, %Y %X %Z', time() );
and
<?php
# locale2.php
error_reporting( E_ALL | E_STRICT );
date_default_timezone_set( 'America/Los_Angeles' );
setlocale( LC_ALL, 'english_usa' ); // awkward Windows locale string
echo strftime( '%A, %B %d, %Y %X %Z', time() );
然后我在两个单独的选项卡中执行它们。第一的locale1.php
,在设置区域设置后休眠 10 秒,让我们有时间执行locale2.php
与此同时。
令我惊讶的是locale2.php
甚至不允许正确更改区域设置。它出现sleep( 10 )
in locale1.php
以不允许的方式劫持 Apache/PHP 进程locale2.php
同时更改区域设置。它does然而,当然,同时回显日期,只是没有像您期望的那样本地化。
Edit:抱歉,废了。它出现locale2.php
does更改区域设置并locale1.php
然后在睡觉后打印英文日期而不是荷兰语日期。以便does似乎符合预期的行为setlocale()
.
/Edit
然后,我测试了IntlDateFormatter
与这两个文件:
<?php
# locale1.php
error_reporting( E_ALL | E_STRICT );
$dateFormatter = new IntlDateFormatter(
'nl_NL',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Amsterdam'
);
sleep( 10 ); // let's sleep for a bit here
echo $dateFormatter->format( time() );
and
<?php
# locale2.php
error_reporting( E_ALL | E_STRICT );
$dateFormatter = new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles'
);
echo $dateFormatter->format( time() );
然后在两个单独的选项卡中再次执行它们,与第一组文件的方式相同。这does给出预期结果:同时locale1.php
在睡觉locale2.php
根据美国规则很好地用美式英语打印日期,然后locale1.php
根据荷兰规则,很好地用荷兰语打印日期。
所以,结论是,看来Intl
是安全的setlocale
问题。
但也介意金贤民的 https://stackoverflow.com/a/11025442/165154答案当然是。由于缺乏使用经验,我无法对此发表评论Intl
。我最近才发现Intl
.