PHP date - get name of the months in local language

后端 未结 5 1557
礼貌的吻别
礼貌的吻别 2020-12-11 00:46

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(\         


        
相关标签:
5条回答
  • 2020-12-11 01:04

    You should use setlocale() and strftime():

    setlocale(LC_TIME, 'sr_CS');
    $month_name = strftime('%B', mktime(0, 0, 0, $i));
    
    0 讨论(0)
  • 2020-12-11 01:04

    For all who struggle with German (and de_DE), make sure you are using the right language code. Login to your server and run locale -a to see a list of all available ones. For me it shows:

    C
    C.UTF-8
    de_AT.utf8
    de_BE.utf8
    de_CH.utf8
    de_DE.utf8
    de_LI.utf8
    de_LU.utf8
    ...

    You need to use one of those codes.

    Then you can use:

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

    and "März 2020" etc. get displayed correctly.

    0 讨论(0)
  • 2020-12-11 01:08

    It is good idea to pass the encoding when setting the locale:

     <?php    
     date_default_timezone_set('Europe/Belgrade');
     setlocale(LC_TIME, array('sr_CS.UTF-8', 'sr.UTF-8'));
    
    0 讨论(0)
  • 2020-12-11 01:13

    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)
    0 讨论(0)
  • 2020-12-11 01:14

    Here is an example with IntlDateFormatter

    $format = new IntlDateFormatter('sr_CS', IntlDateFormatter::NONE, 
                  IntlDateFormatter::NONE, NULL, NULL, "MMM");
    $monthName = datefmt_format($format, mktime(0, 0, 0, $i));
    
    0 讨论(0)
提交回复
热议问题