CakeFest 2024: The Official CakePHP Conference

IntlDateFormatter::setCalendar

datefmt_set_calendar

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

IntlDateFormatter::setCalendar -- datefmt_set_calendarFormatter が使うカレンダー型を設定する

説明

オブジェクト指向型

public IntlDateFormatter::setCalendar(IntlCalendar|int|null $calendar): bool

手続き型

datefmt_set_calendar(IntlDateFormatter $formatter, IntlCalendar|int|null $calendar): bool

このフォーマッターが使用するカレンダーあるいはカレンダー型を設定します。

パラメータ

formatter

フォーマッターリソース。

calendar

使用する カレンダー型 (デフォルトは IntlDateFormatter::GREGORIAN で、 null を指定した場合にもこれを使います)、あるいは IntlCalendar オブジェクト。

IntlCalendar オブジェクトが渡された場合は、それをクローンします。 引数として渡したオブジェクトは、何も変更されません。

フォーマッターのタイムゾーンを残すのは IntlCalendar オブジェクトが渡されなかった場合だけで、 このオブジェクトが渡された場合は新しいタイムゾーンを利用します。

戻り値

成功した場合に true を、失敗した場合に false を返します。

変更履歴

バージョン 説明
5.5.0/PECL 3.0.0 IntlCalendar オブジェクトを渡せるようになりました。

例1 datefmt_set_calendar() の例

<?php
$fmt
= datefmt_create(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo
'calendar of the formatter is : ' . datefmt_get_calendar($fmt);
datefmt_set_calendar($fmt, IntlDateFormatter::TRADITIONAL);
echo
'Now calendar of the formatter is : ' . datefmt_get_calendar($fmt);
?>

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

<?php
$fmt
= new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN
);
echo
'calendar of the formatter is : ' . $fmt->getCalendar();
$fmt->setCalendar(IntlDateFormatter::TRADITIONAL);
echo
'Now calendar of the formatter is : ' . $fmt->getCalendar();
?>

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

calendar of the formatter is : 1
Now calendar of the formatter is : 0

例3 引数に IntlCalendar を使う例

<?php
$time
= strtotime("2013-03-03 00:00:00 UTC");
$formatter = IntlDateFormatter::create("en_US", NULL, NULL, "Europe/Amsterdam");

echo
"before: ", $formatter->format($time), "\n";

/* カレンダーのロケールは使われないことに注目 */
$formatter->setCalendar(IntlCalendar::createInstance(
"America/New_York", "pt_PT@calendar=islamic"));

echo
"after: ", $formatter->format($time), "\n";

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

before: Sunday, March 3, 2013 at 1:00:00 AM Central European Standard Time
after:  Saturday, Rabiʻ II 20, 1434 at 7:00:00 PM Eastern Standard Time

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top