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?
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?
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:
$locale
- A canonicalized locale, you can be sure you're getting the right one by filtering your selected Locale through Locale::canonicalize
.$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.$timeType
- Same as $dateType
but instead dealing with time.$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).$calendar
- Typically Gregorian (null
), but can accept an instance of IntlCalendar
as well.$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, anIntlCalendar
object, a numeric type representing a (possibly fractional) number of seconds since epoch or an array in the format output bylocaltime()
.
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();
}
<?php
setlocale(LC_ALL, 'tr_TR.UTF-8');
echo strftime("%e %B %Y %A", time());
?>
Prints out: 30 Ekim 2015 Cuma
Code works fine.
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
}