PHP date - get name of the months in local language
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('F', mktime(0, 0, 0, $i));
$i is the number of the month (values 1 - 12). See also PHP:mktime.
You should use
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:
sr_BA- Serbian (Montenegro)
sr_CS- Serbian (Serbia)
sr_ME- Serbian (Serbia and Montenegro)
How to display month name in my language, On linux run $ locale -a to find out if the needed locale is installed best way to translate month names to put them in language file and call it� Don't use the PHP date function, use the Drupal API: format_date. To add the comma, make it part of the pattern. To add the comma, make it part of the pattern. To match your example 30 maart 2015, 11: 34 , the pattern is j F Y, H: i :
setlocale(LC_TIME, 'sr_CS'); $month_name = strftime('%B', mktime(0, 0, 0, $i));
Best way to translate month names, Format the time and/or date according to locale settings. Month and weekday names and other language-dependent strings respect the current locale set with � strftime() will get local time using timezone setting from date_default_timezone_set, then send format and converted tm structure to strftime() system call. So it will output time stamp string ("%s" format string) with timezone converted tm structure, and it will be wrong unless you set the same time zone with your machine.
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));
strftime - Manual, date. (PHP 4, PHP 5, PHP 7). date — Format a local time/date if no timestamp is given. In other words, timestamp is optional and defaults to the value of time(). strtotime expects a standard date format, and passes back a timestamp.. You seem to be passing strtotime a single digit to output a date format from.. You should be using mktime which takes the date elements as parameters.
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:
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.
date - Manual, 20 PHP date - get name of the months in local language Dec 12 '12 16 Yii2 - WHERE IN with active record Jul 7 '15 12 Vue 2 - vuex mapGetters and pass params Aug 1 '17
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'));
[Update: for MODX 2.2.4 (or higher) users, see my latest post at the bottom of this page] For < 2.2.4 users, read below: I searched ages for this: How to change the output language of date notations(day and month) in MODx.
When the month name is in a non-English language (such as "mai" in French, which is the same as "may" in English), the bug can be solved by finding a way that would replace month names with their English counterpart, then uses php's strtotime.
When trying to use "pt_BR" on some servers you will ALWAYS get false. Even with other languages. The locale string need to be supported by the server. Sometimes there are diferents charsets for a language, like "pt_BR.utf-8" and "pt_BR.iso-8859-1", but there is no support for a _standard_ "pt_BR". This problem occours in Windows platform too.
This resource lists the supported language, script, and region parts of the locale names. For information about the supported locale names that have non-default sort orders, see the Locale name column in Sort Order Identifiers. Under Windows 10 or later, locale names that correspond to valid BCP-47 language tags are allowed.
strftime, and install the proper locale, & set your current locale to serbian.
- Thank you for your help :)
dateis locale unaware, my
strftimeis not. If yours is locale aware... what's your PHP version?
- But notice that
setlocalesets the locale process wide (causing unexpected behavior with multiple threads when using multi-threading). Thread safe alternative: IntlDateFormatter
- This solution is incorrect. Let's say it's 2019-01-30, the function mktime() without the fifth and sixth parameter will use the current day and year, so if it's 2019-01-30 and you want to know $i = 2 (February), the result will be March, because 2019-02-30 doesn't exist and it will be interpreted as a date in March. To avoid this problem, use for example: mktime(0, 0, 0, $i, 1, 2019). Anyway, don't use this solution, because it will give the wrong result on certain specific dates. So many incorrect answers on Stackoverflow ...
- it has issues with time zone on datetime object if you use setTimezone() and use getTimestamp()
- this work for me, sadly on my machine
- Hello, here please change locale to "en" and month number to 11. Then it shows me dec and not Nov. Can you please guide?