IntlDateFormatter::create

datefmt_create

IntlDateFormatter::__construct

Date Formatter を作成する

説明

オブジェクト指向型

public static IntlDateFormatternull IntlDateFormatter::create(
    stringnull $locale,
    int $dateType = IntlDateFormatter::FULL,
    int $timeType = IntlDateFormatter::FULL,
    IntlTimeZoneDateTimeZonestringnull $timezone = null,
    IntlCalendarintnull $calendar = null,
    stringnull $pattern = null
)

オブジェクト指向型 (コンストラクタ)

public IntlDateFormatter::__construct(
    stringnull $locale,
    int $dateType = IntlDateFormatter::FULL,
    int $timeType = IntlDateFormatter::FULL,
    IntlTimeZoneDateTimeZonestringnull $timezone = null,
    IntlCalendarintnull $calendar = null,
    stringnull $pattern = null
)

手続き型

IntlDateFormatternull datefmt_create(
    stringnull $locale,
    int $dateType = IntlDateFormatter::FULL,
    int $timeType = IntlDateFormatter::FULL,
    IntlTimeZoneDateTimeZonestringnull $timezone = null,
    IntlCalendarintnull $calendar = null,
    stringnull $pattern = null
)

Date Formatter を作成します。

パラメータ

locale

フォーマットやパースの際に使用するロケール。null を指定すると、 ini 設定 intl.default_locale の値を使います。

en_US と en-US のどちらの形式も受け付けます。 ロケールには fa_IR@calendar=persian のように ICU のキーワードを含められますが、 calendar キーワードによって使うカレンダーが変わるのは、 calendar に IntlDateFormatter::TRADITIONAL を指定した場合のみです。
dateType

使用する日付の形式。 IntlDateFormatter の定数 のいずれかです。 デフォルト値は IntlDateFormatter::FULL です。

timeType

使用する時刻の形式。 IntlDateFormatter の定数 のいずれかです。 デフォルト値は IntlDateFormatter::FULL です。

timezone

タイムゾーン ID。デフォルト (そして、null が渡されたときにも使われるもの) は date_default_timezone_get が返す ID、あるいは calendar パラメータに IntlCalendar オブジェクトが渡されていればそのタイムゾーンを使います。 この ID は、ICU のデータベースにある妥当な識別子である必要があります。あるいは、 オフセットを明示した GMT-05:30 のような形式になります。

IntlTimeZone オブジェクトあるいは DateTimeZone オブジェクトも指定できます。

calendar

フォーマットやパースの際に使用するカレンダー。デフォルトは null で、 これは IntlDateFormatter::GREGORIAN を表します。 IntlDateFormatter のカレンダー定数 あるいは IntlCalendar を指定します。 渡された IntlCalendar は、クローンされます。 IntlDateFormatter によって元のオブジェクトが書き換えられることはありません。 これは、利用するカレンダーの型 (グレゴリオ暦、イスラム暦、ペルシア暦など) の判定に使います。 また、timezone パラメータに null を指定した場合にはタイムゾーンの判定にも使います。

IntlDateFormatter::GREGORIAN は、ロケールに calendar キーワードがあっても、常にグレゴリオ暦を使います。 IntlDateFormatter::TRADITIONAL は、 ロケールのデフォルトのカレンダーを使います。 これは、ロケールに calendar キーワードがあればそのカレンダー、 なければ ICU がロケールの地域ごとに定めているカレンダーです。 たとえば fa_IR ではペルシア暦、th_TH では仏暦ですが、 en_US ではグレゴリオ暦です。 地域ごとのカレンダーは ICU のバージョンによって異なります。 ロケールで使えるカレンダーの種類の一覧は、 IntlCalendar::getKeywordValuesForLocale で取得できます。
pattern
フォーマットやパースの際に使用するオプションのパターン。 指定した場合、dateType と timeType で決まる形式の代わりに使います。 使えるパターンについては » https://unicode-org.github.io/icu/userguide/format_parse/datetime/ を参照ください。 タイムゾーンはパターンでは決まりません。 表示される時刻も、z で表示されるタイムゾーン名も、 timezone で指定したタイムゾーンのものです。

戻り値

作成された IntlDateFormatter を返します。失敗した場合は null を返します。

エラー / 例外

locale が不正な場合、 ValueError をスローします。

変更履歴

バージョン 説明
8.4.0 locale が不正な場合、 ValueError をスローするように なりました。
8.1.0

dateType と timeType は、オプションになりました。

例

例1 datefmt_create の例

<?php
$fmt = datefmt_create( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
    'America/Los_Angeles', IntlDateFormatter::GREGORIAN  );
echo "First Formatted output is ".datefmt_format( $fmt , 0);
$fmt = datefmt_create( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
    'America/Los_Angeles',IntlDateFormatter::GREGORIAN  );
echo "Second Formatted output is ".datefmt_format( $fmt , 0);

$fmt = datefmt_create( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
     'America/Los_Angeles',IntlDateFormatter::GREGORIAN  ,"MM/dd/yyyy");
echo "First Formatted output with pattern is ".datefmt_format( $fmt , 0);
$fmt = datefmt_create( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
     'America/Los_Angeles',IntlDateFormatter::GREGORIAN  ,"MM/dd/yyyy");
echo "Second Formatted output with pattern is ".datefmt_format( $fmt , 0);
?>

例2 オブジェクト指向の例

<?php
$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
    'America/Los_Angeles',IntlDateFormatter::GREGORIAN  );
echo "First Formatted output is ".$fmt->format(0);
$fmt = new IntlDateFormatter( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
    'America/Los_Angeles',IntlDateFormatter::GREGORIAN  );
echo "Second Formatted output is ".$fmt->format(0);

$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
     'America/Los_Angeles',IntlDateFormatter::GREGORIAN  ,"MM/dd/yyyy");
echo "First Formatted output with pattern is ".$fmt->format(0);
$fmt = new IntlDateFormatter( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL,
      'America/Los_Angeles',IntlDateFormatter::GREGORIAN , "MM/dd/yyyy");
echo "Second Formatted output with pattern is ".$fmt->format(0);
?>

例3 無効なロケールを処理する例

<?php
try {
    $fmt = new IntlDateFormatter(
        'invalid_locale',
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        'dunno',
        IntlDateFormatter::GREGORIAN,
    );
} catch (\Error $e) {
    // ...
}
?>

上の例の出力は以下となります。

First Formatted output is Wednesday, December 31, 1969 4:00:00 PM PT
Second Formatted output is Mittwoch, 31. Dezember 1969 16:00 Uhr GMT-08:00
First Formatted output with pattern is 12/31/1969
Second Formatted output with pattern is 12/31/1969
         

例4 ロケールのカレンダーと、パターンを指定したときのタイムゾーン

<?php
$timestamp = 1700000000;

foreach ([IntlDateFormatter::GREGORIAN, IntlDateFormatter::TRADITIONAL] as $calendar) {
    $formatter = new IntlDateFormatter(
        'en_US@calendar=persian',
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        'UTC',
        $calendar,
        'yyyy-MM-dd'
    );
    echo $formatter->format($timestamp), "\n";
}

$formatter = new IntlDateFormatter(
    'en_US',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'Asia/Singapore',
    null,
    'yyyy-MM-dd HH:mm zzzz'
);
echo $formatter->format(gmmktime(21, 0, 0, 6, 1, 2024)), "\n";

上の例の出力は以下となります。

2023-11-14
1402-08-23
2024-06-02 05:00 Singapore Standard Time

参考

  • datefmt_format
  • datefmt_parse
  • datefmt_get_error_code
  • datefmt_get_error_message