(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)
IntlDateFormatter::setLenient -- datefmt_set_lenient — 設定剖析器的寬鬆程度
物件導向風格
程序式風格
定義剖析器在解讀與模式不完全相符的輸入時,是否要嚴格或寬鬆。啟用寬鬆剖析允許剖析器接受其他有缺陷的日期或時間模式,盡可能地剖析以取得值。額外的空格、無法辨識的標記或無效的值(例如「2 月 30 日」)將不被接受。
範例 #1 datefmt_set_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 "\n現在格式器的寬鬆模式為: ";
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();
}
?>
範例 #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 嘗試解析('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 "\n現在格式器目前的寬鬆模式設定為:";
if ($fmt->isLenient()) {
echo 'TRUE';
} else {
echo 'FALSE';
}
$fmt->parse('35/13/1971');
echo "\n 嘗試解析('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 : 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