(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)
IntlDateFormatter::isLenient -- datefmt_is_lenient — 取得 IntlDateFormatter 使用的寬鬆模式
物件導向風格
程序式風格
檢查剖析器在解譯與模式不完全相符的輸入時是否為嚴格或寬鬆模式。
formatter
格式化器資源。
範例 #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