Get list of localized months

2020-03-04 07:25发布

Since PHP uses data from ICU in the intl extension, are there ways to get localized month names from within PHP?

While I can get the data from ICU's xml files and extract them into my own set of files, I would much prefer a clean and easier solution if possible.

Finally, are there ways to get the date format (MM/DD/YYYY or DD/MM/YYYY), given the locale from the intl extension?

4条回答
▲ chillily
2楼-- · 2020-03-04 07:54

(This is not a real answer, it would be a comment to Steven R. Loomis' answer. I write it only because I can't format code in comments)

Thank you Steven R. Loomis, this is my solution:

function local_month_name($locale, $monthnum)
{
    /**
     *  Return the localized name of the given month
     *  
     *  @author Lucas Malor
     *  @param string $locale The locale identifier (for example 'en_US')
     *  @param int $monthnum The month as number
     *  @return string The localized string
     */

    $fmt = new IntlDateFormatter($locale, IntlDateFormatter::LONG, 
                                 IntlDateFormatter::NONE);

    $fmt->setPattern('MMMM');
    return $fmt->format(mktime(0, 0, 0, $monthnum, 1, 1970));
}
查看更多
家丑人穷心不美
3楼-- · 2020-03-04 07:59

Could you use IntlDateFormatter::getPattern to get the pattern? I don't know about strftime, but I would use the suggestion of formatting with a pattern MMMM to get the month name, through each month. Looks like php.intl doesn't expose the data directly.

查看更多
萌系小妹纸
4楼-- · 2020-03-04 08:06

Not sure what you need the month names for, but if you just want to use translated names for months, strftime() uses the names according to the current locale (use the %B modifier).

If you want a list of month names for some other use you can use strftime() for that too:

$months = array();

for( $i = 1; $i <= 12; $i++ ) {
    $months[ $i ] = strftime( '%B', mktime( 0, 0, 0, $i, 1 ) );
}

For the second question there might be a native function for it but it's not hard to make one yourself:

$currentDate = strftime( '%x', strtotime( '2011-12-13' ) );

$localFormat = str_replace( 
    array( '13', '12', '2011', '11' ),      
    array( 'DD', 'MM', 'YYYY', 'YY' ),
    $currentDate
);
查看更多
叛逆
5楼-- · 2020-03-04 08:18
setlocale(LC_ALL, 'en_US.ISO_8859-1');

and so on

查看更多
登录 后发表回答