简体   繁体   English

带有语言环境奇怪行为的strftime

[英]strftime with locale strange behaviour

In a project, i want to show dates based on user locale. 在一个项目中,我想显示基于用户区域设置的日期。
Dates are stored in a mysql table, as unix timestaps. 日期以unix时间戳的形式存储在mysql表中。
In order for me to show dates in specific locale I have the following code: 为了让我在特定语言环境中显示日期,我使用以下代码:

// in my config file:
// $setLocale is from database (examples: en_EN.UTF8 or fr_FR.UTF8 or it_IT.UTF8 etc)
setlocale(LC_ALL, $setLocale); 

//in my function file
function my_mb_ucfirst($str, $e='utf-8') 
{
    $fc = mb_strtoupper(mb_substr($str, 0, 1, $e), $e);

return $fc.mb_substr($str, 1, mb_strlen($str, $e), $e);
}

//and in any php page i want to show dates:
$dateFromDb = 1419980400; // example value from table
$date = my_mb_ucfirst(strftime("%b %d, %G", $dateFromDb));

Now the strange part: 现在奇怪的部分是:
the above date 1419980400 gives me two different results. 上面的日期1419980400给了我两个不同的结果。

echo my_mb_ucfirst(strftime("%b %d, %G", 1419980400)); 

this gives Dec 31, 2015 这给出了2015年12月31日

echo date("M d, Y", 1419980400); 

this gives Dec 31, 2014 这给出了2014年12月31日

Can anyone explains to me why this is happening? 谁能向我解释为什么会这样?
Is there any error in the above process? 上述过程中是否有错误?

NOTE: this error/bug i verified it with three different configurations: 注意:我通过三种不同的配置验证了此错误/错误:
apache 1.3 + php 4.3 Apache 1.3 + PHP 4.3
apache 2.0 + php 5.3 Apache 2.0 + PHP 5.3
windows xp + xampp 1.7.4 Windows XP + XAMPP 1.7.4

In the german http://php.net/manual/de/function.strftime.php version there is written: 在德语的http://php.net/manual/de/function.strftime.php版本中写道:

%G ... Besonderheit: entspricht die ISO Wochennummer dem vorhergehenden oder folgenden Jahr, wird dieses Jahr verwendet. %G ...发生了什么:完全死于ISO Wochennummer dem vorhergehenden oder folgenden Jahr,死于Jahr verwendet。

Means: it looks for the week no. 意思是:它寻找星期几。 and takes the previous or following year if the week falls into that. 并在上一周或下一年使用上一年或下一年。

So just take %Y instead of %G 所以只用%Y代替%G

EDIT: 编辑:

The english version http://php.net/manual/en/function.strftime.php tells us: 英文版http://php.net/manual/en/function.strftime.php告诉我们:

%G The full four-digit version of %g %G%g的完整四位数版本

%g Two digit representation of the year going by ISO-8601:1988 standards (see %V) %g按照ISO-8601:1988标准的年份的两位数表示形式(请参见%V)

%V ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week 指定年份的%V ISO-8601:1988周号,从该年的第一周开始,至少要有4个工作日,而星期一是该周的开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM