PHP date() in foreign languages - e.g. Mar 25 Aoû 09

后端 未结 4 1514
小蘑菇
小蘑菇 2020-11-29 06:47

I have a script that needs to display date data to an international audience - e.g.

\"submitted Tue 25 Aug 09\"

Is there an easi

相关标签:
4条回答
  • 2020-11-29 07:04

    I think that the best way to do it with strftime and setlocale functions. But it will not work if your server has no needed locale installed (in current questions it is fr_FR).

    Code bellow throw an exception if locale change will be unsuccessful

    <?php
    
    $result = setlocale(LC_ALL, 'fr_FR');
    
    if($result === false){
        throw new \RuntimeException(
            'Got error changing locale, check if locale is installed on the system'
        );
    }
    
    $dayOfMonth = '%e';
    //if it is Windows we will use %#d as %e is not supported
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
       $dayOfMonth = '%#d';
    }
    
    //Mar 25 Aoû 09 - month shortname, day of month, day shortname, year last two digits
    echo strftime("%b $dayOfMonth %a %y"); 
    
    0 讨论(0)
  • 2020-11-29 07:26

    I think you can't get away from doing so without setting LOCALE:

    <?php
    setlocale(LC_ALL, 'fr_FR');
    
    echo strftime("%A %e %B %Y");
    ?> 
    

    Some details on strftime: http://us2.php.net/manual/en/function.strftime.php

    0 讨论(0)
  • 2020-11-29 07:26

    According to the date function's manual page, you should use setlocale. Methods such as strftime will then use the locale specified. date, however, will not for some reason.

    0 讨论(0)
  • 2020-11-29 07:26

    You might also want to have a look at Zend_Date.

    0 讨论(0)
提交回复
热议问题