0

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.

  • 3
    Possible duplicate of [PHP date() in foreign languages - e.g. Mar 25 Aoû 09](https://stackoverflow.com/questions/1328036/php-date-in-foreign-languages-e-g-mar-25-ao%c3%bb-09) – M. Eriksson Mar 02 '18 at 06:36

2 Answers2

2

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.

huehnerhose
  • 615
  • 9
  • 26
  • Thank you so much, how do you think I can implement that into my code? – Damian Majkut Mar 02 '18 at 10:26
  • Thank you very much. After pasting the code - month name has changed to Polish language (which is great), but name of the day of the week still writes in English. When I use : `echo '
    '; echo $date->format("E"); echo '
    ';` but it just return E
    – Damian Majkut Mar 02 '18 at 12:21
  • I have figured out this: `if (version_compare(PHP_VERSION, '5.3.0', '<')) { exit ('IntlDateFormatter is available on PHP 5.3.0 or later.'); } if (!class_exists('IntlDateFormatter')) { exit ('You need to install php_intl extension.'); } $polishDateFormatter = new IntlDateFormatter( 'pl_PL', IntlDateFormatter::FULL, IntlDateFormatter::NONE ); $now = new DateTime("2013-08-06"); echo $polishDateFormatter->format($now), "\n";` and this is working fine. I'm stuck with displaying the date in the foreach($daterange as $date) now. @huehnerhose – Damian Majkut Mar 04 '18 at 05:29
0

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>';

                            }