PHP 8.3.4 Released!

IntlDateFormatter::format

datefmt_format

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

IntlDateFormatter::format -- datefmt_formatTarih/saat değerini bir dizge olarak biçemler

Açıklama

Nesne yönelimli kullanım

public IntlDateFormatter::format(IntlCalendar|DateTimeInterface|array|string|int|float $değer): string|false

Yordamsal kullanım

Zaman değerini bir dizge olarak biçemler.

Bağımsız Değişkenler

biçemleyici

Biçemleyici nesne.

değer

Biçemlenecek değer. Bu bir DateTimeInterface nesnesi, bir IntlCalendar nesnesi, Unix zaman başlangıcından itibaren saniye sayısını (muhtemelen kesirli) temsil eden sayısal bir tür veya localtime() tarafından çıktılanan biçimde bir dizi olabilir.

Değer olarak DateTime veya IntlCalendar nesnesi iletilirse, zaman dilimi dikkate alınmaz. Nesne, biçimlendiricinin yapılandırılmış zaman dilimi kullanılarak biçimlendirilir. Biçimlendirilecek nesnenin zaman dilimi kullanılmak istenirse, IntlDateFormatter::setTimeZone() nesnenin zaman dilimiyle çağrılmalıdır. Veya, bunun yerine IntlDateFormatter::formatObject() statik işlevi de kullanılabilir.

Dönen Değerler

Bir hata oluşmuşsa false aksi takdirde biçemli dizge.

Sürüm Bilgisi

Sürüm: Açıklama
7.1.5 değer bağımsız değişkenine genel DateTimeInterface nesnelerinin aktarımı artık destekleniyor. Evvelce, sadece uygun DateTime nesneleri desteklenirdi.
PECL 3.0.0 değer bağımsız değişkenine artık IntlCalendar nesneleri belirtilebiliyor.

Örnekler

Örnek 1 - datefmt_format() örneği

<?php
$fmt
= datefmt_create(
"tr_TR",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN
);
echo
"İlk biçemli çıktı: ".datefmt_format($fmt, 1234567890);

$fmt = datefmt_create(
"en-US",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN
);
echo
"\nİkinci biçemli çıktı: ".datefmt_format($fmt, 1234567890);

$fmt = datefmt_create(
"tr_TR",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN,
"dd/MM/yyyy"
);
echo
"\nİlk kalıbın çıktısı: ".datefmt_format( $fmt, 1234567890);

$fmt = datefmt_create(
"en-US",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN,
"MM/dd/yyyy"
);
echo
"\nİkinci kalıbın çıktısı: ".datefmt_format($fmt, 1234567890);
?>

Örnek 2 - Nesne yönelimli kullanım örneği

<?php
$fmt
= new IntlDateFormatter(
"tr_TR",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN
);
echo
"İlk biçemli çıktı: ".$fmt->format(1234567890);

$fmt = new IntlDateFormatter(
"en-US",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN
);
echo
"\nİkinci biçemli çıktı: ".$fmt->format(1234567890);

$fmt = new IntlDateFormatter(
"tr_TR",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN,
"dd/MM/yyyy"
);
echo
"\nİlk kalıbın çıktısı: ".$fmt->format(1234567890);

$fmt = new IntlDateFormatter(
"en-US",
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Istanbul',
IntlDateFormatter::GREGORIAN,
"MM/dd/yyyy"
);
echo
"\nİkinci kalıbın çıktısı: ".$fmt->format(1234567890);
?>

Yukarıdaki örneğin çıktısı:

İlk biçemli çıktı: 14 Şubat 2009 Cumartesi 01:31:30 Doğu Avrupa Standart Saati
İkinci biçemli çıktı: Saturday, February 14, 2009 at 1:31:30 AM Eastern European Standard Time
İlk kalıbın çıktısı: 14/02/2009
İkinci kalıbın çıktısı: 02/14/2009

Örnek 3 - IntlCalendar nesnesi ile

<?php
$tz
= reset(iterator_to_array(IntlTimeZone::createEnumeration('FR')));
$formatter = IntlDateFormatter::create(
'tr_TR',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
$tz,
IntlDateFormatter::GREGORIAN
);

$cal = IntlCalendar::createInstance($tz, '@calendar=islamic-civil');
$cal->set(IntlCalendar::FIELD_MONTH, 8); //9. ay, Ramazan
$cal->set(IntlCalendar::FIELD_DAY_OF_MONTH, 1); //İlk gün
$cal->clear(IntlCalendar::FIELD_HOUR_OF_DAY);
$cal->clear(IntlCalendar::FIELD_MINUTE);
$cal->clear(IntlCalendar::FIELD_SECOND);
$cal->clear(IntlCalendar::FIELD_MILLISECOND);

echo
"İslami yılda Ramazan başlangıcı:\n\t",
$formatter->format($cal), "\n";

// Biçemleyici zaman dilimi değişirse
$formatter->setTimeZone('Asia/Tokyo');
echo
"Zaman dilimi değiştiğinde:\n\t",
$formatter->format($cal), "\n";

// Örnek hatalı... Gün doğru; saat yanlış.

Yukarıdaki örneğin çıktısı:

İslami yılda Ramazan başlangıcı:
        13 Nisan 2021 Salı 15:00:00 Orta Avrupa Yaz Saati
Zaman dilimi değiştiğinde:
        13 Nisan 2021 Salı 22:00:00 Japonya Standart Saati

Ayrıca Bakınız

add a note

User Contributed Notes 4 notes

up
44
mail dot dennisbecker at gmail dot com
11 years ago
You should know that PHPs IntlDateFormatter class uses ISO date format codes instead of PHPs date() format codes. It is not really clear mentioned from my point of view.

A good list to find ISO codes is available at http://framework.zend.com/manual/1.12/en/zend.date.constants.html#zend.date.constants.selfdefinedformats and such a list should be added here, too.
up
14
con at bartrail dot de
12 years ago
I hope this will save some time for others who have to struggle with different php versions on dev and prod plattform in the future:

when formatting a DateTime object with a *custom pattern*, be sure to use a timestamp to pass at the IntlDateFormatter::format in order to have it working on different php versions:

Example for PHP Version 5.3.5-1ubuntu7.2 (my dev machine):
<?php
$date
= new \DateTime();

$dateFormatter = \IntlDateFormatter::create(
\Locale::getDefault(),
\IntlDateFormatter::NONE,
\IntlDateFormatter::NONE,
\date_default_timezone_get(),
\IntlDateFormatter::GREGORIAN,
'EEEE'
);

var_dump($dateFormatter->format($date)); // string(6) "Monday"
?>

Example for PHP Version 5.3.2-1ubuntu4.9 (the prod server):
<?php
// same formatting as above

var_dump($dateFormatter->format($date)); // bool(false)
?>

When using $dateFormatter->format($date->getTimestamp()), you'll always get the formatted and localized string instead of a false.
up
0
alex
13 years ago
It's important to note that a conversion of timezones will be made from the default timezone (date_default_timezone_set()) and the timezone you passed in the constructor (or datefmt_create()).

If you are inserting the dates into your database as UTC time, make sure to set date_default_timezone_set to UTC as well (or any other timezone, but they need to be the same). Once you call ::format, you will get the converted time.
To Top