I configured my timezone to Europe/Paris in php.ini. When executing date_default_timezone_get() I do get the correct value.
Then, I expect strftime('%x', date()) to output something like 16 novembre 2018 which is the French format. But instead, I get 11/16/2018 which looks like the US format.
Any idea why?
The time zone has no effect on how dates and times are presented, for that you need to set the locale. There are no standards for locale names, but fortunately PHP's setlocale() function will take multiple locale names, stopping at the first successful one.
// just a few common name formats
setlocale(LC_TIME, ["fr_FR.utf8", "fr_FR#euro", "fr_UTF8", "fr_FR", "french"]);
echo strftime("%d %B %Y", time());
I tried:
setlocale(LC_ALL, 'fr_utf8');
echo strftime("%d %B %Y", time());
and got:
16 novembre 2018
after setting the timezone to Europe/Paris, Use echo date('d-M-Y') and it will display your desired time. it worked for me
Related
My timestamps are stored in a row which is named "date". Then i convert this to readable date and i see encoding problem.
I used setlocale and strftime functions.
Example date stored in table: 2019-09-11 09:57:22
My readable date function is
static function readableDate($date)
{
setlocale(LC_TIME, 'tr_TR');
return strftime('%e %B %Y', strtotime($date));
}
Output is: "11 Eyl�l 2019". It should be "11 Eylül 2019"
Eylül means September. What should I do?
Maybe you need to use the UTF variant of this Turkish locale. It might be picking up ISO one.
what is the output of this:
locale -a | grep tr_TR
You may have to install tr_TR.UTF-8 one and see if that one works.
setlocale(LC_TIME, 'tr_TR.UTF-8', 'tr_TR', 'Turkish');
I live in Bangladesh. But when I use this code.
<?php setlocale(LC_ALL,"asia/dhaka");
echo strftime("%d %B %Y"); ?>
Result shows US Timezone. I want to change my timezone.
Locale and time zone are not the same concept. For example, one can have a US English locale and have their clock set to Japan's local time (perhaps they are traveling).
For PHP, use the date_default_timezone_set function to set the time zone - not the setlocale function.
I am using this simple script to test on my host which runs on php version 5.6.
The script gives the output "Wednesday" instead off "Woensdag"(dutch)
Why is the day of the week still in English and not in Dutch?
Is this because the server is not correct configured?
<?php
/* Set locale to Dutch */
date_default_timezone_set("Europe/Amsterdam");
setlocale(LC_ALL, 'nl_NL');
echo date("l"); // output: Wednesday instead off "Woensdag" (dutch)
?>
Unfortunately date is not multilingual. If you want to format a language in another language you need to set locale (as you did in your example) and use strftime
Formatting options for strftime do result in "Woensdag" (or "mittwoch in German, etc):
setlocale(LC_TIME, 'en_EN');
echo strftime('%A', time()); // for a Wednesday will output: Wednesday
setlocale(LC_TIME, 'nl_NL');
echo strftime('%A', time()); // for a Wednesday will output: woensdag
Unless you want al lot of things to be influenced by setlocale you might want to specify that you're doing this for time only (as in the example above). Read on setlocale for more info: http://php.net/manual/en/function.setlocale.php
Keep in mind you need to have the locales available on your machine for this to work. Check with locale -a on linux. setlocale has a return value. If it is false something went wrong and you're most likely missing the specified locale.
I'm using Codeigniter for display dates.
I have this but it's echoed in english, how do I set it to other language? I already have the spanish language pack, but I can't figure it out how to load it.
$this->load->helper('date');
echo mdate("%F %d, %Y", strtotime(now()));
Thanks
// Set locale to Spanish Argentina, change to your installed language
setlocale(LC_TIME, 'es_AR');
echo strftime("%B %d, %Y", time());
First you do not need to use the date helper to accomplish this. mdate() is essentially the same as strftime() where just the vars are slightly different. Also your code strtotime(now()) is wrong. now() is the exact same as time() as indicated in the CI documentation. It returns a UNIX timestamp, strtotime() converts a string to a UNIX timestamp. So what you were doing was trying to convert a timestamp to a timestamp, which of course is wrong. I changed the code so it doesn't require the helper and correctly will output MONTHNAME DATE, YEAR in the specified language. Make sure you change the es_AR to whatever Spanish language you installed. It will be the in the format es_COUNTRYCODE
The traductions dates in "calendar_lang.php"
$this->config->set_item('language', 'spanish');
$this->load->library('calendar');
echo 'Mes:'.$this->calendar->get_month_name(date('m'));
echo 'Día:'.$this->calendar->get_day_names(date('d'));
I'm brazilian and there's a wordpress plugin that uses
" . date("d F Y (H:i)",$date) . "
Output: 16 January 2013 (00:54)
But it should be 16 Janeiro 2013 (00:54), in portuguese... How can I change it?
PS: I think maybe the date is set by an external file provided by the plugin creator :p I'm not sure though
WordPress has date_i18n to retrieve the date in localized format, based on timestamp.
Try:
echo date_i18n("d F Y (H:i)", $timestamp);
WordPress has an extensive page on how to format date and time.
For the french language I use this
setlocale(LC_ALL, 'fra');
echo strftime("%A %d %B %Y",time());
For in portuguese
setlocale(LC_ALL, 'ptg'); //
echo strftime("%A %d %B %Y",time());
see Language Strings Country/Region Strings.
The documentation for date already answers this:
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().
And strftime says that the way to do what is by using setlocale:
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 setlocale().
That said, the C locale-aware functions do not provide sufficient functionality for languages that have cases. In such situations (i.e. most of the time) you need to roll your own.