PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 29, 2021

[FIXED] setlocale() not working on localhost using MAMP on MacOS

 December 29, 2021     localhost, mamp, php, setlocale     No comments   

Issue

My problem in short: I am using setlocale(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 includes de_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!


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>


Answered By - Kinglish
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing