PHP Conference Japan 2024

IntlDateFormatter::setLenient

datefmt_set_lenient

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

IntlDateFormatter::setLenient -- datefmt_set_lenient设置解析器的宽松度

描述

面向对象风格

public IntlDateFormatter::setLenient(bool $lenient): void

过程化风格

datefmt_set_lenient(IntlDateFormatter $formatter, bool $lenient): void

定义解析器在解释与模式不完全匹配的输入时是严格还是宽松。启用宽松解析允许解析器接受其他有缺陷的日期或时间模式,尽可能多地进行解析以获得值。额外的空格、无法识别的标记或无效值(例如“二月30日”)不被接受。

参数

formatter

格式化器资源

lenient

设置解析器是否宽松,默认为true(宽松)。

返回值

成功返回true,失败返回false

示例

示例 #1 datefmt_set_lenient() 示例

<?php
$fmt
= datefmt_create(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
'dd/MM/yyyy'
);
echo
'lenient of the formatter is : ';
if (
$fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
datefmt_parse($fmt, '35/13/1971');
echo
"\n Trying to do parse('35/13/1971').\nResult is : " . datefmt_parse($fmt, '35/13/1971');
if (
intl_get_error_code() != 0) {
echo
"\nError_msg is : " . intl_get_error_message();
echo
"\nError_code is : " . intl_get_error_code();
}
datefmt_set_lenient($fmt, false);
echo
"\nNow lenient of the formatter is : ";
if (
$fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
datefmt_parse($fmt, '35/13/1971');
echo
"\nTrying to do parse('35/13/1971').\nResult is : " . datefmt_parse($fmt, '35/13/1971');
if (
intl_get_error_code() != 0) {
echo
"\nError_msg is : ".intl_get_error_message();
echo
"\nError_code is : ".intl_get_error_code();
}

?>

示例 #2 面向对象示例

<?php
$fmt
= new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
'dd/MM/yyyy'
);
echo
'lenient of the formatter is : ';
if (
$fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
$fmt->parse('35/13/1971');
echo
"\n Trying to do parse('35/13/1971').\nResult is : " . $fmt->parse('35/13/1971');
if (
intl_get_error_code() != 0) {
echo
"\nError_msg is : " . intl_get_error_message();
echo
"\nError_code is : " . intl_get_error_code();
}

$fmt->setLenient(FALSE);
echo
"\nNow lenient of the formatter is : ";
if (
$fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
$fmt->parse('35/13/1971');
echo
"\n Trying to do parse('35/13/1971').\nResult is : " . $fmt->parse('35/13/1971');
if (
intl_get_error_code() != 0) {
echo
"\nError_msg is : " . intl_get_error_message();
echo
"\nError_code is : " . intl_get_error_code();
}

?>

以上示例将输出

lenient of the formatter is : TRUE
Trying to do parse('35/13/1971').
Result is : 66038400
Now lenient of the formatter is : FALSE
Trying to do parse('35/13/1971').
Result is : 
Error_msg is : Date parsing failed: U_PARSE_ERROR
Error_code is : 9

参见

添加备注

用户贡献的备注

此页面没有用户贡献的备注。
To Top