In a template I display the day and month of a specific date :
<div class="jour"><?php echo date('d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo date('M',strtotime($content->getCreatedAt())) ?></div>
This works fine, problem is the month name is in English. Where do I specify that I want the month names in another locale, French for instance ?
Symfony has a format_date helper among the Date helpers that is i18n-aware. The formats are unfortunately badly documented, see this link for a hint on them.
default_culture only applies for the symfony internationalisation framework, not for native PHP functions. If you want to change this setting project wide, I would do so in config/ProjectConfiguration.class.php, using setlocale, and then use strftime rather than date:
// config/ProjectConfigration.class.php
setlocale(LC_TIME, 'fr_FR');
// *Success.php
<div class="jour"><?php echo strftime('%d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo strftime('%b',strtotime($content->getCreatedAt())) ?></div>
Note that this requires locale settings to be enabled on your machine. To check, do var_dump(setlocale(LC_ALL, 'fr_FR')); If the result is false, you cannot use setlocale to do this and probably need to write the translation code yourself. Furthermore, you will need to have the correct locale installed on your system. To check what locales are installed, do locale -a at the command line.
Sorry for butting in so late in the day, but I would like to add my own thoughts here. The best international date format that I have come up with is "%e %b %Y", e.g. 9 Mar 2012. I find this much more readable than the ISO format "%Y-%m-%d", e.g. 2012-03-09. According to the docs, the %x format should be locale sensitive, but it does not work for me, at least not on the iPhone. This may be because Safari is not passing the locale in the HTML headers, I do not know.
It is sometimes useful to use an array with different possible values to setlocale().
Especially to support different environments (windows, linux, ...)
setlocale(LC_TIME, array('fr', 'fr_FR', 'fr_FR.utf8', 'french', 'french_FRANCE', 'french_FRANCE.utf8'));
echo strftime("%A %d %B", strtotime(date("Y-m-d")));
As the documentation states:
If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.
Related
The strftime() function according to this section of the PHP docs, should be used when trying to use a different language
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().
When using setlocale() with the following parameters
setlocale(LC_ALL, "es_ES", 'Spanish_Spain', 'Spanish');
and strftime()
$mes = strftime('%B', strtotime($semana->semana . "+{$diaPos} days"));
returns the month in english.
One solution I could approach is allocating a constant variable pointing the translations vs the months given by this function, so I would be able to return the correct name.
But it doesn't seem very elegant to me.
I'm using PHP7 on a debian based distro.
In order to use locale you need to have it generated in your system. On debian you should run as root:
dpkg-reconfigure locales
and then your script should work.
When using the PHP to return the timezone abbreviation with either date('T') or strftime('%Z') the result does not translate to the localized version of these abbreviations.
In french I have been told the following at the correct translations:
EDT == HAE
EST == HNE
I have tried the following code example:
<?php
setlocale(LC_ALL, 'fr_FR');
echo strftime('%Z');
echo date('T');
?>
All attempts produce EST/EDT rather than the translated versions.
I get the same on Mac and Linux.
I inspected the Gettext MO file for French, like this:
/usr/local/bin/msgunfmt /usr/share/locale/fr/LC_MESSAGES/texinfo.mo
This shows all the translations for things like months and days.
So you should get translations if you do:
echo strftime("%A %e %B %Y\n");
// gives "Vendredi 27 juin 2014"
But the translation file does not appear to contain translations for EST, or EDT - or in fact any others I looked for.
So I guess the answer is that these strings are simply not translated in the standard locale packages.
I use CakeTime::niceshort() and it works very well. I set correct locale values and set translate (po) files and I can see localized month, day names successfully.
But one thing is missing. When I use niceshort():
echo $this->Time->niceShort(1387120620);
I get this:
Ara 15th 2013, 15:17
But this isn't valid for Turkish dates. I need this:
Ara 15 2013, 15:17
It is possible with these:
$this->Time->format($time,"%b %e %Y, %H:%M")
But I don't want to lose niceShort's features. It gives short information if timestamp is close to now:
https://github.com/cakephp/cakephp/blob/2.5/lib/Cake/Utility/CakeTime.php#L391
Is it possible to remove "th" and "nd" from dates without changing core lib ?
You should use setlocale before calling CakeTime::niceshort()
setlocale("LC_ALL", "tr_TR.UTF-8");
You need to make sure they are properly installed on the server.
I'm using setlocale to display dates in Russian.
setlocale(LC_TIME, 'ru_RU');
My question is, will ru_RU be enough or is it safer to provide a number of alternative language params?
Well, you want dates in Russian, you've set the locale for time-related functions to ru_RU, so that's basically it.
I'd advise you to set the current locale as a secondary choice for LC_TIME, in case ru_RU is not available :
setlocale(LC_TIME, 'ru_RU', setlocale(LC_TIME, '0'));
// setlocale() with '0' will return the current settings without applying changes.
The current locale setting was set by the system administrator, and it is very unlikely to go unavailable.
setlocale() affects a lot of things you should answer yourself if these things covers your needs.
It has two params setlocale ( int $category , array $locale )
$ategory can be(from manual) set to one of these constants:
LC_ALL for all of the below
LC_COLLATE for string comparison, see strcoll()
LC_CTYPE for character classification and conversion, for example strtoupper()
LC_MONETARY for localeconv()
LC_NUMERIC for decimal separator (See also localeconv())
LC_TIME for date and time formatting with strftime()
LC_MESSAGES for system responses (available if PHP was compiled with libintl)
Also look at this quote from manual:
Returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.
I have this MySQL query, which returns two dates (which are both formatted as a-m-Y). Now I want to translate this date into my own language (Danish). How can I do that.
I have tried both the setlocale() and strftime() functions, but it won't work.
I know it's a very basic question, but i really need help :) Thanks a lot!
I found that setlocale isn't reliable, as it is set per process, not per thread (the manual mentions this). This means other running scripts can change the locale at any time. A solution is using IntlDateFormatter from the intl php extension.
Install intl if necesarry (ubuntu): sudo apt-get install php5-intl
Install the locale you want to use (I'm using italian as an example): sudo locale-gen it_IT
Generate a locally formatted date:
$fmt = new \IntlDateFormatter('it_IT', NULL, NULL);
$fmt->setPattern('d MMMM yyyy HH:mm');
// See: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax for pattern syntax
echo $fmt->format(new \DateTime());
// Output: 6 gennaio 2016 12:10
Use setlocale and strftime together:
setlocale(LC_TIME, array('da_DA.UTF-8','da_DA#euro','da_DA','danish'));
echo strftime("%A"); // outputs 'tirsdag'
Works on my php installation on Windows.
strftime(): Warning! This function has been DEPRECATED as of PHP 8.1.0. Relying on this function is highly discouraged.
Use
http://php.net/manual/en/function.strftime.php
<?php
setlocale(LC_ALL, 'da_DA');
echo strftime("%A %e %B %Y");
?>
I don't think the date() function is quite evolved enough for you, here.
Instead, I would recommend you take a look at the IntlDateFormatter1 class (quoting) :
Date Formatter is a concrete class that enables locale-dependent
formatting/parsing of dates using pattern strings and/or canned
patterns.
There are a couple of examples on the manual page of IntlDateFormatter::format(), where that method is used to display a date in two different languages, by just setting the desired locale.
1. bundled with PHP >= 5.3
This Will Surely works for you if you want norwegian date and month format
$date = '2016-11-16 05:35:14';
setlocale(LC_TIME, array('nb_NO.UTF-8','nb_NO#norw','nb_NO','norwegian'));
echo strftime("%e %b %Y",strtotime($date));
if you want to get other language locale ids like nb_NO then refer this site
International Components for Unicode (ICU) Data
If you are trying to convert a datetime try this:
$fecha = $dateConsulta->format('d-M-Y');
$fecha = str_replace('Jan','Ene',$fecha);
$fecha = str_replace('Apr','Abr',$fecha);
$fecha = str_replace('Aug','Ago',$fecha);
$fecha = str_replace('Dec','Dic',$fecha);