Turkish Encoding problem while converting timestamp - php

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');

Related

PHP strftime outputs wrong format despite correct timezone

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

Php mysql date in table

I have a date field in my table material
id: int;
dat_operation : date;
name : varchar(255);
I would like to know how to translate date format in French
I tried with:
<?php echo date("F j Y",strtotime($var['date_operation']));?>
But i have this result
June 14 2016
First, you'll have to set the "locale information", to specify which language you want to use. Keep in mind, that even though you set that language, it needs to be installed on the server you're running on. It most likely is, but you'll notice if the setlocale has no effect (default is English).
The second thing you'll need to know, is that date() isn't affected by this, you'll have to use strftime() instead, which has a slightly different formatting, which you'll find on the documentation.
An example of using French dates with these two functions:
setlocale(LC_ALL, 'fr_FR');
echo strftime("%B %e %Y", strtotime($var['date_operation']));
Reference and documentation:
http://php.net/manual/en/function.setlocale.php
http://php.net/manual/en/function.strftime.php
The modern and rock-solid approach is the intl (from "Internationalization") extension, which offers e.g. the IntlDateFormatter class:
$date = new DateTime('2016-06-14');
$fmt = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE, 'Europe/Paris', IntlDateFormatter::GREGORIAN);
var_dump($fmt->format($date));
string(12) "14 juin 2016"
If you think it's overkill for your project, you can use the legacy strftime() function but you need to change current locale:
$date = strtotime('2016-06-14');
var_dump(setlocale(LC_TIME, 'fr_FR', 'fr')); // Need to try values until you get true
var_dump(strftime('%B %e %Y', $date));
You need to have French locale data installed. In my experience, this works better on Unix-like systems than on Windows.

how to set date and time to dutch

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.

Set the Date / Time locale to de_AT instead of de_DE on Microsoft Azure Website (PHP)

I am currently developing a a Website that will be hosted on Microsoft Azure Websites. The client (as well as I) live in Austria and thus we would like to have the Austrian (German) locale for time and date - there is a small difference in the dates as we say "Jänner" instead of "Januar" for "January" and "Januar" sounds somewhat strange to an austrian. The standard snippet for returning a german date would be like so:
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_DE.utf8','de_DE', 'de_DE#euro', 'german');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
The answer to this question is acutlly fairly simple. To have austrian or swiss date formats, you can change the locales to the corresponding languages.
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_AT.utf8','de_AT', 'de_AT#euro', 'german_austria');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
for austrian
or
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_CH.utf8','de_CH', 'de_CH#euro', 'german_switzerland');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
for swiss german. (note that there is not much difference in the regional format between german and switzerland)
But please note, that german_austria outputs Jänner, which sometimes can render as J�nner as the last locale ('german_austria', 'german' or 'german_switzerland') is not an UTF8 Locale and 'german-austria.utf8' is not available on my host.
If your system has no UTF-8 compatible locale installed and you can not install a nother one, you can simply wrap the strftime function into the utf8_encode() function from PHP and everything should work like a charm.

Translate date("d F Y (H:i) function php

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.

Categories