Damian Majkut
Damian Majkut

Reputation: 1

Change language of php months and days

it's my first question here so please excuse me for any technical mistakes.

We are creating a WP website for a cinema. We have set up a calendar carousel and I'm struggling to get the days and months tranlated from Fri, Sat, Sun etc. to Polish language. Here is the code:

 <?php    
                            $begin = new DateTime('today');
                            $end = new DateTime('+ 19 days');
                            $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);


                        foreach($daterange as $date){
                            echo '<div class="carousel-item">';
                            echo '<a href="#DAY'.$date->format("d").'" data-toggle="tab" class="dka-link-kal">';
                            echo '<div class="day-div" id="DD'.$date->format("d").'">';

                            echo '<div class="nazwa-dnia">';
                            echo $date->format("D");
                            echo '</div>';
                            echo '<div class="numer-dnia">';
                            echo $date->format("d");
                            echo '</div>';
                            echo '<div class="nazwa-miesiaca">';
                            echo $date->format("M");
                            echo '</div>';
                            echo '</div>';
                            echo '</div>';
                            echo '</a>';

                            }
                            ?>

I think I don't have locale installed on the actual server. I will be really glad if you can give me a hint. Thanks.

Upvotes: 0

Views: 1452

Answers (2)

Damian Majkut
Damian Majkut

Reputation: 1

Ok, I have figured out this with the arrays:

$dzien_tyg_pl2 = array('Mon' => 'Pon', 'Tue' => 'Wt', 'Wed' => 'Śr', 'Thu'=> 'Czw', 'Fri' => 'Pt', 'Sat' => 'Sb', 'Sun' => 'Nd'); 
$miesiac_pl2 = array(1 => 'Sty', 'Lut', 'Mar', 'Kwi', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrze', 'Paź', 'Lis', 'Gru');

                        setlocale(LC_ALL, 'pl-PL');
                        foreach($daterange as $date){
                            echo '<div class="carousel-item">';
                            echo '<a href="#DAY'.$date->format("d").'" data-toggle="tab" class="dka-link-kal">';
                            echo '<div class="day-div" id="DD'.$date->format("d").'">';

                            echo '<div class="nazwa-dnia">';
                            echo $dzien_tyg_pl2[$date->format("D")];
                            echo '</div>';
                            echo '<div class="numer-dnia">';
                            echo $date->format("d");
                            echo '</div>';
                            echo '<div class="nazwa-miesiaca">';
                            echo $miesiac_pl2[$date->format("n")];
                            echo '</div>';
                            echo '</div>';
                            echo '</div>';
                            echo '</a>';

                            }

Upvotes: 0

huehnerhose
huehnerhose

Reputation: 635

After many roadblocks I came to the solution using IntlDateFormatter

My first test was using setlocale but this ends in undesired sideeffects. IntlDateFormatter has rich formatting options following the ICU API

EDIT: As starting point:

    $formatter = new IntlDateFormatter(
        Locale::acceptFromHttp(
            isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])
                ? $_SERVER['HTTP_ACCEPT_LANGUAGE']
                : 'de-DE'
        ),
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        null,
        null,
        $format
    );

    echo $formatter->format($begin);

Since I use dateformatting all over my app, I wrote a little wrapper exposing just dayDateString(DateTime $d) : string and handling the formatter generation internally.

Upvotes: 2

Related Questions