PHP Conference Japan 2024

IntlDateFormatter::isLenient

datefmt_is_lenient

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

IntlDateFormatter::isLenient -- datefmt_is_lenient取得 IntlDateFormatter 使用的寬鬆模式

說明

物件導向風格

public IntlDateFormatter::isLenient(): bool

程序式風格

datefmt_is_lenient(IntlDateFormatter $formatter): bool

檢查剖析器在解譯與模式不完全相符的輸入時是否為嚴格或寬鬆模式。

參數

formatter

格式化器資源。

回傳值

如果剖析器為寬鬆模式則為true,如果剖析器為嚴格模式則為false。預設情況下,剖析器為寬鬆模式。

範例

範例 #1 datefmt_is_lenient() 範例

<?php
$fmt
= datefmt_create(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
'dd/mm/yyyy'
);
echo
'格式器的寬鬆模式設定為:';
if (
$fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
datefmt_parse($fmt, '35/13/1971');
echo
"\n 嘗試剖析('35/13/1971')。\n結果為:" . datefmt_parse($fmt, '35/13/1971');
if (
intl_get_error_code() != 0) {
echo
"\n錯誤訊息為:" . intl_get_error_message();
echo
"\n錯誤碼為:" . intl_get_error_code();
}
datefmt_set_lenient($fmt,false);
echo
'現在格式器的寬鬆模式設定為:';
if (
$fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
datefmt_parse($fmt, '35/13/1971');
echo
"\n 嘗試剖析('35/13/1971')。結果為:" . datefmt_parse($fmt, '35/13/1971');
if (
intl_get_error_code() != 0) {
echo
"\n錯誤訊息為:" . intl_get_error_message();
echo
"\n錯誤碼為:" . intl_get_error_code();
}

?>

範例 #2 物件導向範例

<?php
$fmt
= new IntlDateFormatter(
'en_US',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'America/Los_Angeles',
IntlDateFormatter::GREGORIAN,
"dd/mm/yyyy"
);
echo
"格式器的寬鬆模式為:"
if ($fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
$fmt->parse('35/13/1971');
echo
"\n 嘗試執行 parse('35/13/1971').\n結果為:" . $fmt->parse('35/13/1971');
if (
intl_get_error_code() != 0){
echo
"\n錯誤訊息為:" . intl_get_error_message();
echo
"\n錯誤碼為:" . intl_get_error_code();
}

$fmt->setLenient(FALSE);
echo
'現在格式器的寬鬆模式為:';
if (
$fmt->isLenient()) {
echo
'TRUE';
} else {
echo
'FALSE';
}
$fmt->parse('35/13/1971');
echo
"\n 嘗試執行 parse('35/13/1971').\n結果為:" . $fmt->parse('35/13/1971');
if (
intl_get_error_code() != 0) {
echo
"\n錯誤訊息為:" . intl_get_error_message();
echo
"\n錯誤碼為:" . intl_get_error_code();
}

?>

以上範例會輸出:

lenient of the formatter is : TRUE
Trying to do parse('35/13/1971').
Result is : -2147483
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