php - setlocale() not working on localhost using MAMP on MacOS

My problem in short: I am usingsetlocale(LC_TIME, "de_DE")
in order to display the "verbal" parts of a date/time (i.e. month, weekday) in German. This works on any public server, but doesn't on my localhost, using MAMP, which displays it in English instead of German.
In detail:
I have the following code (nothing else in the file, I reduced it to the minimum necessary to reproduce the issue):
<?php
setlocale(LC_TIME, "de_DE");
date_default_timezone_set('Europe/Berlin');
?>
<!DOCTYPE html>
<html lang='de_DE'>
<head>
<title>Datum in Deutsch</title>
<meta charset="UTF-8">
</head>
<body>
<p>
<?php
echo "Heute ist ";
echo strftime("%A, der %e. %B %Y");
?>
</p>
</body>
</html>
This should be displayed as
Heute ist Donnerstag, der 10. Juni 2021
But on my localhost/MAMP, it is displayed as
Heute ist Thursday, der 10. June 2021
As I mentioned: If I upload this file to any public server and open it, it is displayed as desired (i.e. with the German expressions)
I have searched SO and other websites for a solution. An advice I found several times was to check if the German locale is installed at all on my system.
So I opened the terminal (I am on MacOS 10.14.6) and typed "locale -a": The list of returned and installed locales includesde_DE
(among many others).
I also found the advice to include this line in the code to check whether the desired locale is available, which I did:
<?php echo setlocale(LC_ALL, 0); ?>
This outputs "C/C/C/C/de_DE/C " , so again,de_DE
is included (but won't give me a localized date display).
My system: MacOS 10.14.6, MAMP 6.3 as local Apache server, running PHP 7.4.12 (switching to PHP 8.0 changes nothing)
Edit/additional info: I previously had used AMPPS as a local server on the same system, where this worked. So to me it seems to be a MAMP issue.
What can I do to make it work? Any advice appreciated!
Answer
Solution:
Have you tried IntlDateFormatter? This uses unicode date formatting. View the example in action
$fmt = datefmt_create( "de-DE",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Berlin',
IntlDateFormatter::GREGORIAN,
"EEEE, 'der' d. LLLL Y");
echo "Heute ist " . datefmt_format( $fmt ,time());
//output: Heute ist Freitag, der 18. Juni 2021
In context
<?php
setlocale(LC_TIME, "de_DE");
date_default_timezone_set('Europe/Berlin');
?>
<!DOCTYPE html>
<html lang='de_DE'>
<head>
<title>Datum in Deutsch</title>
<meta charset="UTF-8">
</head>
<body>
<p>
<?php
$fmt = datefmt_create( "de-DE" , IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Berlin',IntlDateFormatter::GREGORIAN ,"EEEE, 'der' d. LLLL Y");
echo "Heute ist " . datefmt_format( $fmt ,time());
?>
</p>
</body>
</html>
Answer
Solution:
Put this in place of lines 1 to 4:
<?php setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu'); ?>
Answer
Solution:
try install/reinstall language package 1stsudo /usr/share/locales/install-language-pack de_DE
and then set your language usingsetlocale()
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.