使用PHP的date_get_last_errors()函数返回解析日期字符串时检测到的警告/错误

date_get_last_errors()函数可以返回分析日期字符串时检测到的警告和错误。

基本语法

date_get_last_errors()

环境

  • Windows 10

  • XAMPP 8.0.6

  • PHP 版本 8.0.6

date_get_last_errors() 函数

让我们编写一个脚本,使用 PHP 的 date_get_last_errors() 函数返回在解析日期字符串时发现的警告和错误。

使用PHP的date_get_last_errors()函数返回解析日期字符串时检测到的警告/错误  第1张

代码

<html>
<head>
<title>date_get_last_errors()函数返回解析日期字符串时检测到的警告/错误</title>
</head>
<body>
<?php
date_create("dmsakn4-~=3*;s");
print_r(date_get_last_errors());
?>
</body>
</html>

使用date_create()函数来创建一个新的DateTime 对象。在函数的括号内,作为参数,本来是要传递日期/时间的字符串,作为测试我们写一些非日期的字符,使用 print_r() 函数打印返回的警告和错误的详细信息。

执行

将此脚本另存为“test.php”并运行它。

使用PHP的date_get_last_errors()函数返回解析日期字符串时检测到的警告/错误  第2张

Array ( [warning_count] => 1 [warnings] => Array ( [13] => Double timezone specification ) [error_count] => 8 [errors] => Array ( [0] => The timezone could not be found in the database [6] => Unexpected character [7] => Unexpected character [8] => Unexpected character [9] => Unexpected character [10] => Unexpected character [11] => Unexpected character [12] => Unexpected character ) )

当我让它运行时,通过使用date_get_last_errors()函数返回了date_create()函数解析DateTime 对象中的字符串所包含错误/警告信息的数组

本文来源:词雅网

本文地址:https://www.ciyawang.com/date_get_last_errors.html

本文使用「 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 」许可协议授权,转载或使用请署名并注明出处。

相关推荐