Get a list of localized months

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

While I can get data from ICU xml files and extract it into my own set of files, I would prefer a simple and simple solution, if possible.

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

+4
source share
4 answers

Could you use IntlDateFormatter::getPattern to get the template? I don't know about strftime, but I would use a formatting sentence with an MMMM template to get the month name every month. It looks like php.intl does not provide data directly.

+4
source

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

If you need a list of month names for other uses, you can also use strftime() for this:

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

There may be a native function for the second question, but this is not difficult to do:

 $currentDate = strftime( '%x', strtotime( '2011-12-13' ) ); $localFormat = str_replace( array( '13', '12', '2011', '11' ), array( 'DD', 'MM', 'YYYY', 'YY' ), $currentDate ); 
+4
source

(This is not a real answer, it will be a comment on the answer of Stephen R. Lomis. I write this only because I can not format the code in the comments)

Thanks 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
source
 setlocale(LC_ALL, 'en_US.ISO_8859-1'); 

etc.

-1
source

Source: https://habr.com/ru/post/915114/


All Articles