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
"创建 GregorianCalendar 后\n";
print_r(getSetFields($cal));
echo
"\n";

echo
IntlDateFormatter::formatObject($cal), "\n";
echo
"格式化程序请求扩展年份后\n";
print_r(getSetFields($cal));
echo
"\n";

$cal->clear(IntlCalendar::FIELD_YEAR);
echo
"年份被清除后,日期保持不变\n";
echo
IntlDateFormatter::formatObject($cal), "\n";
echo
"因为 FIELD_EXTENDED_YEAR 仍然已设置\n";
print_r(getSetFields($cal));
echo
"\n";

var_dump($cal->clear(IntlCalendar::FIELD_EXTENDED_YEAR));
echo
"扩展年份被清除后\n";
print_r(getSetFields($cal));
echo
IntlDateFormatter::formatObject($cal), "\n";
echo
"\n";

echo
"字段重新计算后,\n"
. "扩展年份再次设置(为 1970)\n";
print_r(getSetFields($cal));
echo
"\n";

$cal->clear();
echo
"调用无参数的变体后\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