Php get month name in different languages

This question asks how to get a list of months, I only see hints, not a complete code answer so:

If you have IntlDateFormatter available - which is available in most of the cases, you can create a formatter in a given locale and repeatedly push a date to it created just based on month number

// or any other locales like pl_PL, cs_CZ, fr_FR, zh, zh_Hans, ...
$locale = 'en_GB';
$dateFormatter = new IntlDateFormatter[
    $locale,
    IntlDateFormatter::LONG, // date type
    IntlDateFormatter::NONE  // time type
];
$dateFormatter->setPattern['LLLL']; // full month name with NO DECLENSION ;-]
$months_locale = [];
for [$month_number = 1; $month_number format[
        // 'n' => month number with no leading zeros
        DateTime::createFromFormat['n', [string]$month_number]
    ];
}
// test output
echo "
";
var_dump[$months_locale];
echo "
";

Note: LLLL takes care of not-declining, but it does not take care of the lowercase/uppercase of the first letter if the languages has such things.
Good example is that you can get January for en_GB but leden for cs_CZ
If you want all letters lowercase => use mb_strtolower[$month_name]; - docs
If you want just the FIRST letter to be upper case =>
=> use mb_convert_case[$month_name, MB_CASE_TITLE, 'UTF-8']; - docs

Always use mb_* functions or their variations for locale-originating strings !

So no, don't use ucfirst !

I'm doing this:

{# Get events from `events` section in desired sort order #}
{% set allEvents = craft.entries.section['events'].order['eventsDate asc'] %}

{# Loop through `allEvents` and print the list #}
{% for event in allEvents %}
    
  • {{ event.title }} - {{ event.eventsDate|date['j F, Y'] }}
  • {% endfor %}

    I guess I can do something with this: //php.net/manual/en/datetime.formats.date.php, but I need to display months in my native language, which is slightly different from how Craft outputs it.

    For example I'd like it to say "7 des", not "07 dec", "15 mai", not "15th may" and so on.

    carlcs

    35.9k5 gold badges58 silver badges135 bronze badges

    asked Jul 15, 2014 at 17:38

    3

    Yes, the date[] filter you use [see Twig docs] allows you to format dates with the same formating characters as the php date function takes. All language dependent parts are returned in the language of the current locale. I don't know if you can change that primary locale in the control panel, but you could do that in the DB.

    As you noted in the comments to your question, you want the CP to be in english. This is possible even if you set the site's locale to another language; the site's locale and the user language for the CP are different things.

    answered Jul 15, 2014 at 20:36

    carlcscarlcs

    35.9k5 gold badges58 silver badges135 bronze badges

    3

    1. Home
    2. Date
    3. Php Date Get Name Of The Months In Local Language

    PHP date - get name of the months in local language

    Tags: date , php , localization Answers: 1 | Viewed 67,887 times

    I have this part of the function, which gives me name of the months in English. How can I translate them to my local language [Serbian]?


    $month_name = date['F', mktime[0, 0, 0, $i]];

    Where $i is the number of the month [values 1 - 12]. See also PHP:mktime.


    MAXIM answer at 2012-12-12 28

    You should use setlocale[]:


    setlocale[LC_TIME, 'fr_FR'];
    $month_name = date['F', mktime[0, 0, 0, $i]];

    In this case it would set it to French. For your case it should be one of the following:



    1. sr_BA - Serbian [Montenegro]

    2. sr_CS - Serbian [Serbia]

    3. sr_ME - Serbian [Serbia and Montenegro]


    * The answers/resolutions are collected from stackoverflow, are licensed under CC BY-SA 3.0

    Some Code Answers


    $month_name = date['F', mktime[0, 0, 0, $i]];

    setlocale[LC_TIME, 'fr_FR'];
    $month_name = date['F', mktime[0, 0, 0, $i]];

    setlocale[LC_TIME, 'sr_CS'];
    $month_name = strftime['%B', mktime[0, 0, 0, $i]];

    $format = new IntlDateFormatter['sr_CS', IntlDateFormatter::NONE,    IntlDateFormatter::NONE, NULL, NULL, "MMM"];
    $monthName = datefmt_format[$format, mktime[0, 0, 0, $i]];

    date_default_timezone_set['Europe/Berlin'];
    setlocale[LC_ALL, 'de_DE.utf8'];
    $date_now = date['Y-m-d'];
    $month_available = strftime['%B %Y', strtotime[$date_now]];
    $month_next = strftime['%B %Y', strtotime[$date_now.' +1 month']];

    // or any other locales like pl_PL, cs_CZ, fr_FR, zh, zh_Hans, ... $locale = 'en_GB';
    $dateFormatter = new IntlDateFormatter[
    $locale,
    IntlDateFormatter::LONG, // date type
    IntlDateFormatter::NONE // time type ];
    $dateFormatter->setPattern['LLLL'];
    // full month name with NO DECLENSION ;-] $months_locale = [];
    for [$month_number = 1;
    $month_number format[
    // 'n' =>
    month number with no leading zeros
    DateTime::createFromFormat['n', [string]$month_number]
    ];
    } // test output echo "
    ";
    var_dump[$months_locale];
    echo "
    ";

     

    Chủ Đề