PHP Setlocale Has No Effect

PHP setlocale has no effect

This solution might help if you don't have shell access to the server.

If you have shell access, then Benjamin Seiller's answer is the best!

As I don't have any other possibilities (shell), I've found a solution with only PHP by using the IntlDateFormatter class.

<?php

// Example vars
$month = '6';
$year = '2014';

$fmt = new IntlDateFormatter('de_DE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Berlin',
IntlDateFormatter::GREGORIAN);

$lastMonth = mktime(0, 0, 0, $month -1, 1, $year);

$showLastMonth = $fmt->format($lastMonth);
echo $showLastMonth;

setlocale having no effect in PHP

Do you have the de locale available; what does setlocale return for you? See: return values for setlocale().

Also, check the list of available locales (e.g. locale -a or whatever is suitable for your OS) to see if de is among them. Likely alternatives include de_DE or de_DE.utf8 to name a few.

In Debian, to generate a new locale, run this command:

dpkg-reconfigure locales

and pick the ones you want.

PHP setlocale not working even with locales existing

okay, after posting this, I tried one more thing. So for those experiencing the same issue, you need to set this first before setting the new locale:

setlocale(LC_TIME, "");

setLocale not working with any language, correct local is installed

Don't be fooled by the ".utf8" part of the locale. Setlocale is for single byte functions only, they don't work well with Unicode.
Also, it matters which operating system you are using, and depending on the operating system, it matters if you use the TS or NTS build of PHP.

For internationalisation / Unicode I'd recommend using the Intl extention, it has full Unicode support and a very complete calendar IntlCalendar.

PHP setlocale() fails in browser but works in cli

After you install new locales you need to restart PHP for the new locales to work. So try restarting PHP.

PHP: date F with setlocale not working

date() and DateTime do not respect locale

use strftime()

http://php.net/manual/en/function.strftime.php

http://php.net/manual/en/function.date.php

To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().

setlocale() not working properly with date()

From the manual about date()

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

So use strftime():

print strftime("%a %d-%m-%Y");

To sum up always one day, just use a timestamp like:

for ($time = time(), $contador = 1;
$contador <= 7;
$contador++, $time = strtotime('+1 day', $time)) {
print strftime("%a %d-%m-%Y", $time)."<br />\n";
}


Related Topics



Leave a reply



Submit