PHP Conference Japan 2024

IntlCalendar::clear

(PHP 5 >= 5.5.0, PHP 7, PHP 8, PECL >= 3.0.0a1)

IntlCalendar::clear清除一個欄位或所有欄位

說明

物件導向風格

public IntlCalendar::clear(?int $field = null): true

程序式風格

intlcal_clear(IntlCalendar $calendar, ?int $field = null): true

清除所有欄位或特定欄位。已清除的欄位會標記為未設定,使其在計算時間時,相較於重疊欄位或甚至預設值具有最低優先級。此外,它的值會設定為 0,但由於該欄位的優先級低,在查詢欄位完成之前,它的值可能已在內部設定為其他值。

參數

calendar

一個 IntlCalendar 實例。

field

IntlCalendar 日期/時間 欄位常數 之一。這些是介於 0IntlCalendar::FIELD_COUNT 之間的整數值。

返回值

總是返回 true

更新日誌

版本 說明
8.2.0 返回類型現在是 true;以前是 bool

範例

範例 #1 IntlCalendar::clear() 範例

<?php
ini_set
('intl.default_locale', 'es_ES');
ini_set('date.timezone', 'UTC');

$fields = array(
'FIELD_ERA' => 0,
'FIELD_YEAR' => 1,
'FIELD_MONTH' => 2,
'FIELD_WEEK_OF_YEAR' => 3,
'FIELD_WEEK_OF_MONTH' => 4,
'FIELD_DATE' => 5,
'FIELD_DAY_OF_YEAR' => 6,
'FIELD_DAY_OF_WEEK' => 7,
'FIELD_DAY_OF_WEEK_IN_MONTH' => 8,
'FIELD_AM_PM' => 9,
'FIELD_HOUR' => 10,
'FIELD_HOUR_OF_DAY' => 11,
'FIELD_MINUTE' => 12,
'FIELD_SECOND' => 13,
'FIELD_MILLISECOND' => 14,
'FIELD_ZONE_OFFSET' => 15,
'FIELD_DST_OFFSET' => 16,
'FIELD_YEAR_WOY' => 17,
'FIELD_DOW_LOCAL' => 18,
'FIELD_EXTENDED_YEAR' => 19,
'FIELD_JULIAN_DAY' => 20,
'FIELD_MILLISECONDS_IN_DAY' => 21,
'FIELD_IS_LEAP_MONTH' => 22,
'FIELD_FIELD_COUNT' => 23,
);
function
getSetFields(IntlCalendar $cal) {
global
$fields;
$ret = array();
foreach (
$fields as $name => $value) {
if (
$cal->isSet($value)) {
$ret[] = $name;
}
}
return
$ret;
}

$cal = new IntlGregorianCalendar(2013, 2 /* March */, 15);
echo
"After GregorianCalendar creation\n";
print_r(getSetFields($cal));
echo
"\n";

echo
IntlDateFormatter::formatObject($cal), "\n";
echo
"After the formatter requested the extended year\n";
print_r(getSetFields($cal));
echo
"\n";

$cal->clear(IntlCalendar::FIELD_YEAR);
echo
"After the year has been cleared, the date stays the same\n";
echo
IntlDateFormatter::formatObject($cal), "\n";
echo
"because FIELD_EXTENDED_YEAR is still set\n";
print_r(getSetFields($cal));
echo
"\n";

var_dump($cal->clear(IntlCalendar::FIELD_EXTENDED_YEAR));
echo
"After the extended year has been cleared\n";
print_r(getSetFields($cal));
echo
IntlDateFormatter::formatObject($cal), "\n";
echo
"\n";

echo
"After the fields are recalculated,\n"
. " extended year is set again (to 1970)\n";
print_r(getSetFields($cal));
echo
"\n";

$cal->clear();
echo
"After calling variant with no arguments\n";
print_r(getSetFields($cal));
echo
IntlDateFormatter::formatObject($cal), "\n";

以上範例將輸出

After GregorianCalendar creation
Array
(
    [0] => FIELD_ERA
    [1] => FIELD_YEAR
    [2] => FIELD_MONTH
    [3] => FIELD_DATE
)

15/03/2013 00:00:00
After the formatter requested the extended year
Array
(
    [0] => FIELD_ERA
    [1] => FIELD_YEAR
    [2] => FIELD_MONTH
    [3] => FIELD_DATE
    [4] => FIELD_EXTENDED_YEAR
)

After the year has been cleared, the date stays the same
15/03/2013 00:00:00
because FIELD_EXTENDED_YEAR is still set
Array
(
    [0] => FIELD_ERA
    [1] => FIELD_MONTH
    [2] => FIELD_DATE
    [3] => FIELD_EXTENDED_YEAR
)

bool(true)
After the extended year has been cleared
Array
(
    [0] => FIELD_ERA
    [1] => FIELD_MONTH
    [2] => FIELD_DATE
)
15/03/1970 00:00:00

After the fields are recalculated,
 extended year is set again (to 1970)
Array
(
    [0] => FIELD_ERA
    [1] => FIELD_MONTH
    [2] => FIELD_DATE
    [3] => FIELD_EXTENDED_YEAR
)

After calling variant with no arguments
Array
(
)
01/01/1970 00:00:00

新增註釋

使用者貢獻的註釋

此頁面沒有使用者貢獻的註釋。
To Top