PHP date - get name of the months in local language
You should use setlocale()
:
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)
getting always german month-names in PHP
I popose to use this, where you create an own class, that does all the work in the constructor and offers only one simple-to-use function (but still felxible to change the patten)
class Formatter {
private $dateFormatter;
public function __construct() {
$formatter = new \IntlDateFormatter(
"de-DE",
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM,
"Europe/Berlin");
$this->dateFormatter = $formatter;
}
public function printDate(\DateTime $dateTime, string $pattern = null) {
if ($pattern) {
$this->dateFormatter->setPattern($pattern);
}
return $this->dateFormatter->format($dateTime);
}
}
usage
$fmt = new Formatter();
echo $fmt->printDate($now, "d. MMM");
How to display month name in my language
You can use strftime
(http://php.net/manual/en/function.strftime.php):
setlocale(LC_TIME, array('ro.utf-8', 'ro_RO.UTF-8', 'ro_RO.utf-8', 'ro', 'ro_RO', 'ro_RO.ISO8859-2'));
$luna=$_GET['month'];
$an=$_GET['year'];
$dateObj = DateTime::createFromFormat('!m', $luna);
$monthName = strftime('%B', $dateObj->getTimestamp());
$data='Arhiva '.$monthName.' - '.$an.'';
Best way to translate month names
You can use str_ireplace()
with arrays for names in english, and equivalent for names in turkmen, in same order.
Something like this:
$nmeng = array('january', 'february', ..);
$nmtur = array('ocak', 'subat', ...);
$dt = 'September 22, 2012';
$dt = str_ireplace($nmeng, $nmtur, $dt);
PHP Date, International month names?
Use setlocale
and strftime
to get a localized version of the date string.
Example from PHP Docs - http://php.net/manual/en/function.setlocale.php :
/* Set locale to Dutch */
setlocale(LC_ALL, 'nl_NL');
/* Output: vrijdag 22 december 1978 */
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));
/* try different possible locale names for german as of PHP 4.3.0 */
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
echo "Preferred locale for german on this system is '$loc_de'";
Name of day/month that's not today in another language in php
Whenever you need to manipulate date/time stamps based on locale, you should use strftime
also change the encoding to utf-8
http://php.net/manual/en/function.strftime.php
example :
<?php
header('Content-Type: text/html; charset=UTF-8');
$myDate = "Feb 21, 2013";
$locale = 'fr_FR.utf8';
setlocale(LC_ALL, $locale);
echo strftime('%d %B %Y', strtotime($myDate));
$locale = 'en_US.utf8';
setlocale(LC_ALL, $locale);
echo strftime('%d %B %Y', strtotime($myDate));
?>
Convert number to month name in PHP
The recommended way to do this:
Nowadays, you should really be using DateTime objects for any date/time math. This requires you to have a PHP version >= 5.2. As shown in Glavić's answer, you can use the following:
$monthNum = 3;
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F'); // March
The !
formatting character is used to reset everything to the Unix epoch. The m
format character is the numeric representation of a month, with leading zeroes.
Alternative solution:
If you're using an older PHP version and can't upgrade at the moment, you could this solution.
The second parameter of date()
function accepts a timestamp, and you could use mktime()
to create one, like so:
$monthNum = 3;
$monthName = date('F', mktime(0, 0, 0, $monthNum, 10)); // March
If you want the 3-letter month name like Mar
, change F
to M
. The list of all available formatting options can be found in the PHP manual documentation.
Related Topics
How to Disable Adding Properties into a Class from an Instance of the Class
Routes in Codeigniter - Automatically
PHP Upload Size and Its Impact on Post Size and Memory Limit
Sort Array by Value Alphabetically PHP
Swap Two Words in a String PHP
Php: "Short_Open_Tag = On" Not Working
How to Update Timezonedb in PHP (Updating Timezones Info)
Reply-To Address in PHP Contact Form
How to Examine Defined Constants in PHP
Cookies Not Working on Different Pages
Counting Values in Multidimensional Array
PHP $_Get and $_Post Undefined Problem
Reverse Order of String Like "Hello Word" Reverse as "Word Hello" in PHP
How to Ensure I Caught All Errors from MySQLi::Multi_Query
Android: Uploading Image on Server with PHP
How to Add a PHP Simplexmlelement to Another Simplexmlelement