Reputation: 2098
In a project, i want to show dates based on user locale.
Dates are stored in a mysql table, as unix timestaps.
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.
echo my_mb_ucfirst(strftime("%b %d, %G", 1419980400));
this gives Dec 31, 2015
echo date("M d, Y", 1419980400);
this gives Dec 31, 2014
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 2.0 + php 5.3
windows xp + xampp 1.7.4
Upvotes: 1
Views: 560
Reputation: 12675
In the german http://php.net/manual/de/function.strftime.php version there is written:
%G ... Besonderheit: entspricht die ISO Wochennummer dem vorhergehenden oder folgenden Jahr, wird dieses 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
EDIT:
The english version http://php.net/manual/en/function.strftime.php tells us:
%G The full four-digit version of %g
%g Two digit representation of the year going by ISO-8601:1988 standards (see %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
Upvotes: 5