Get list of localized months
Asked Answered
T

5

4

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?

Tortuous answered 29/8, 2011 at 10:26 Comment(0)
P
4

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.

Prokofiev answered 29/8, 2011 at 22:29 Comment(3)
Initially I thought getPattern only returns the custom pattern if one is defined. This does not seem to be the case, and getPattern works perfectly for the date format. I prefer not to use setlocale as it is quite inconsistent. Therefore, I have decided to just extract month names out of the ICU data.Tortuous
by extract, you mean using the APIs to format each month, right? Please, please, don't read ICU data directly.Prokofiev
Is there a way to do this using PHP? I have checked the intl extension, and there doesn't seem to be any apis to get the data. What I have done is extracted data out of unicode.org's xml files (which I believe is sourced from ICU) into a bunch of PHP files which I can use during runtime.Tortuous
F
5

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
);
Ferrochromium answered 29/8, 2011 at 10:59 Comment(0)
P
4

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.

Prokofiev answered 29/8, 2011 at 22:29 Comment(3)
Initially I thought getPattern only returns the custom pattern if one is defined. This does not seem to be the case, and getPattern works perfectly for the date format. I prefer not to use setlocale as it is quite inconsistent. Therefore, I have decided to just extract month names out of the ICU data.Tortuous
by extract, you mean using the APIs to format each month, right? Please, please, don't read ICU data directly.Prokofiev
Is there a way to do this using PHP? I have checked the intl extension, and there doesn't seem to be any apis to get the data. What I have done is extracted data out of unicode.org's xml files (which I believe is sourced from ICU) into a bunch of PHP files which I can use during runtime.Tortuous
H
4

(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));
}
Himyaritic answered 6/6, 2013 at 11:31 Comment(1)
DECLENSION DANGER you probably want $fmt->setPattern('LLLL'); format if you are looking JUST for the month NAMES with no declension, MMMM actually gets declined because it's created from a whole date - so e.g. instead of Leden you get Ledna (Leden in English is always just January)Cochabamba
W
0

For English or other languages that does not use declination it will work fine, but for all of them that use declination the output will not be valid.

For Polish i.e. the output for March will be marca instead of marzec For Russian i.e. the output for March will be Мартa instead of Март

This method will not work through ALL languages which most probably is required and may not be easily tested by person not speaking the language.

If you only need names of the months it is better to use:

setlocale(LC_TIME, 'pl_PL');
$month_name = strftime('%B', mktime(0, 0, 0, $month_number));

Please have in mind that locale change may impact other parts of your application.

Witch answered 6/12, 2020 at 15:12 Comment(0)
S
-1
setlocale(LC_ALL, 'en_US.ISO_8859-1');

and so on

Sciolism answered 29/8, 2011 at 10:39 Comment(3)
are there any other alternatives to using setlocale? I find it to be quite inconsistent across different operating systems.Tortuous
operating system of what? Server?Sciolism
The application will run on linux and potentially on windows servers as well. In the future, it is possible that it will be deployed on other other operating systems (solaris, etc).Tortuous

© 2022 - 2024 — McMap. All rights reserved.