0

I have already installed Intl to my server.

I'v changed my default_timezone to Europe/Istanbul and intl.default_locale to tr_TR.

I'm still getting the date names in English.

What am i missing?

Should i change some other settings?

Canser Yanbakan
  • 3,780
  • 3
  • 39
  • 65
  • What happens if you add `setlocale(LC_TIME, 'tr_TR');` at the top of your script? – trizz Jun 25 '14 at 15:01
  • @trizz Nothing changes... I don't want to use strftime function. Using new Datetime and format to show my date. – Canser Yanbakan Jun 25 '14 at 15:05
  • `setlocale` changes the locale for the entire script and all time/date functions, not only `strftime`. You could check if the locale `tr_TR` is installed at your server with `locale -a` in your SSH shell. If it isn't installed, it will fallback to English. – trizz Jun 25 '14 at 15:17
  • Yes it is. I can see tr_TR and tr_TR.utf8 on the list... – Canser Yanbakan Jun 25 '14 at 15:20

3 Answers3

4

I recommend using IntlDateFormatter. This is available in PHP's intl module, and judging by the title of this question, this extension is available.

A sample/rough implementation:

$locale = 'tr_TR'; // a canonicalized locale
$format = 'MMM e'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
    $locale, // string locale
    IntlDateFormatter::NONE, // int date type
    IntlDateFormatter::NONE, // int time type
    'UTC', // string timezone
    IntlDateFormatter::GREGORIAN, // int cal type
    $format // string pattern
);
var_dump($dt->format('c')); // string '2014-06-25T08:22:48-07:00' (length=25)
var_dump($df->format($dt)); // string 'Haz 3' (length=5)

First and foremost, using any of the Intl* classes is beneficial because you don't need to constantly reset PHP's runtime locale (this can get messy IMO) with setlocale or Locale::setDefault. This allows you to easily output/format with different locales in a very structured way. It also pushes for use of PHP5's new OOPness -- no more of that date nonsense, you should be using DateTime now.

The IntlDateFormatter constructor has a fairly long signature, as follows:

  1. $locale - A canonicalized locale, you can be sure you're getting the right one by filtering your selected Locale through Locale::canonicalize.
  2. $dateType - I set this to 'none' (int -1) and use the format parameter ($pattern) for maximum flexibility. Accepts 'short', 'medium', 'long', and 'full' as well. You can read more about them in the ICU project docs.
  3. $timeType - Same as $dateType but instead dealing with time.
  4. $timezone - This is not an instance of DateTimeZone, but instead a string timezone. PHP's Date classes are not compatible with intl's classes (the exception is with certain built-in adapters such as the IntlDateFormatter::format method).
  5. $calendar - Typically Gregorian (null), but can accept an instance of IntlCalendar as well.
  6. $pattern - Parsing/Rendering format to use, think of this as the equivalent to date's first parameter. However, they are not the same: ICU (the underling lib that this class uses) employs an ISO format that is well documented in the Zend Framework docs. For example, instead of using 'Y' for a 4 digit year, you would use 'yyyy'. The ISO format is much more robust than PHP's.

Once you have an instance of IntlDateFormatter, as well as a DateTime object, you can glue the two together to render out a string containing the date/time you want in the given locale with the given format (as previously defined) using IntlDateFormatter::format. The format method accepts a single argument: an instance of DateTime -- piece of cake!

$instanceOfIntlDateFormatter->format($instanceOfDateTime);

As a bonus the format method can take a wide variety of objects (of type mixed), quote:

Value to format. This may be a DateTime object, an IntlCalendar object, a numeric type representing a (possibly fractional) number of seconds since epoch or an array in the format output by localtime().

A note about the timezone parameter: DateTime changed at version PHP 5.5.0. I have used the following as a workaround (can't remember the details, work it out yourself)...

$timezoneForIntl = $instanceOfDateTime->getTimezone();
if ( version_compare(PHP_VERSION,'5.5.0','<') ) {
    $timezoneForIntl = $timezoneForIntl->getName();
}
zamnuts
  • 9,492
  • 3
  • 39
  • 46
1
      <?php
        setlocale(LC_ALL, 'tr_TR.UTF-8');
        echo strftime("%e %B %Y %A", time());
      ?>

Prints out: 30 Ekim 2015 Cuma

Code works fine.

Vurkac
  • 133
  • 8
0
public function getTurkishDate(){
    $locale = 'tr_TR'; // a canonicalized locale
    $format = 'dd-MMMM-YYYY'; // ISO format codes, not the typical date ones
    $dt = new DateTime(); // a DateTime object
    $df = new IntlDateFormatter(
        $locale, // string locale
        IntlDateFormatter::NONE, // int date type
        IntlDateFormatter::NONE, // int time type
        'UTC', // string timezone
        IntlDateFormatter::GREGORIAN, // int cal type
        $format // string pattern
    );
    return $df->format($dt); //string 07-Ağustos-2018
} 
Tolga
  • 147
  • 3
  • 4
  • Welcome to SO and kudos for diving straight in to answer a question. In order to be of maximum help to people who come across your answer later, it's useful to add some explanation rather than just code. See [answer]. – peeebeee Aug 07 '18 at 08:11