未捕获的异常:DateTime::__construct():无法解析时间字符串
Uncaught Exception: DateTime::__construct(): Failed to parse time string
我正在使用下面的函数根据生日(欧洲格式 DD/MM/YYYY)计算人们的年龄(以年为单位),这些日期作为文本存储在 Wordpress 高级自定义字段中
function get_age($birthDate_old) {
$birthDate = ($birthDate_old);
return date_diff(new DateTime($birthDate), new DateTime('today'))->y;
}
在大多数情况下它工作正常但在某些情况下我收到以下错误:
Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (26/01/1958) at position 0 (2): Unexpected character in /home/XXXX/functions.php:99 Stack trace: #0 /home/XXXX/functions.php(99): DateTime->__construct('26/01/1958') #1 /home/XXXX/single.php(69): get_age('26/01/1958') #2 /home/XXX/wp-includes/template-loader.php(98): include('/home/XXX/...') #3 /home/XXX/wp-blog-header.php(19): require_once('/home/XXX/...') #4 /home/XXX/index.php(17): require('/home/monmaire/...') #5 {main} thrown in /home/XXXX/functions.php on line 99
正常工作的数据示例:
$年龄 = get_age($生日);
对于 $birthday value = 05/04/1946 它工作正常但是对于 $birthday value = 26/01/1958 我得到上面的错误。
我不明白为什么在我看来这两种情况下的数据格式相同。
你知道为什么吗?
谢谢。
问候。
据此:
https://www.php.net/manual/en/datetime.formats.php
// THIS IS INVALID, WOULD IMPLY MONTH == 19
$external = "19/10/2016 14:48:21";
// HOWEVER WE CAN INJECT THE FORMATTING WHEN WE DECODE THE DATE
$format = "d/m/Y H:i:s";
$dateobj = DateTime::createFromFormat($format, $external);
$iso_datetime = $dateobj->format(Datetime::ATOM);
echo "SUCCESS: $external EQUALS ISO-8601 $iso_datetime";
26 被视为月份。因此,像上面一样将 $format = "d/m/Y";
添加到您的日期。
我这样修复你的功能:
function get_age($birthDate_old) {
return date_diff(
DateTime::createFromFormat('d/m/Y', $birthDate_old),
new DateTime('today')
)->y;
}
在您的情况下,您的日期格式不正确,因为 DateTime 构造函数不知道您是过了一个月还是一天。
05/04/1946 可以。
26/01/1958 不行 因为26默认代表月份
日期格式有问题。我相信你在“/”和“-”之间切换。如果您处理的是大数据,则很难修复整个数据库,所以我最好的建议是执行 preg_replace 以始终确保您从 user/DB 中获取的内容是格式正确。在下面找到示例代码。
function get_age($birthDate_old) {
$birthDate = ($birthDate_old);
return date_diff(new DateTime($birthDate), new DateTime('today'))->y;
}
$birthDate = "26/01/1958";
$birthDate = preg_replace("(/)", "-", $birthDate);
echo get_age($birthDate);
希望对您有所帮助!让我知道
我正在使用下面的函数根据生日(欧洲格式 DD/MM/YYYY)计算人们的年龄(以年为单位),这些日期作为文本存储在 Wordpress 高级自定义字段中
function get_age($birthDate_old) {
$birthDate = ($birthDate_old);
return date_diff(new DateTime($birthDate), new DateTime('today'))->y;
}
在大多数情况下它工作正常但在某些情况下我收到以下错误:
Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (26/01/1958) at position 0 (2): Unexpected character in /home/XXXX/functions.php:99 Stack trace: #0 /home/XXXX/functions.php(99): DateTime->__construct('26/01/1958') #1 /home/XXXX/single.php(69): get_age('26/01/1958') #2 /home/XXX/wp-includes/template-loader.php(98): include('/home/XXX/...') #3 /home/XXX/wp-blog-header.php(19): require_once('/home/XXX/...') #4 /home/XXX/index.php(17): require('/home/monmaire/...') #5 {main} thrown in /home/XXXX/functions.php on line 99
正常工作的数据示例: $年龄 = get_age($生日);
对于 $birthday value = 05/04/1946 它工作正常但是对于 $birthday value = 26/01/1958 我得到上面的错误。 我不明白为什么在我看来这两种情况下的数据格式相同。
你知道为什么吗?
谢谢。 问候。
据此:
https://www.php.net/manual/en/datetime.formats.php
// THIS IS INVALID, WOULD IMPLY MONTH == 19
$external = "19/10/2016 14:48:21";
// HOWEVER WE CAN INJECT THE FORMATTING WHEN WE DECODE THE DATE
$format = "d/m/Y H:i:s";
$dateobj = DateTime::createFromFormat($format, $external);
$iso_datetime = $dateobj->format(Datetime::ATOM);
echo "SUCCESS: $external EQUALS ISO-8601 $iso_datetime";
26 被视为月份。因此,像上面一样将 $format = "d/m/Y";
添加到您的日期。
我这样修复你的功能:
function get_age($birthDate_old) {
return date_diff(
DateTime::createFromFormat('d/m/Y', $birthDate_old),
new DateTime('today')
)->y;
}
在您的情况下,您的日期格式不正确,因为 DateTime 构造函数不知道您是过了一个月还是一天。
05/04/1946 可以。
26/01/1958 不行 因为26默认代表月份
日期格式有问题。我相信你在“/”和“-”之间切换。如果您处理的是大数据,则很难修复整个数据库,所以我最好的建议是执行 preg_replace 以始终确保您从 user/DB 中获取的内容是格式正确。在下面找到示例代码。
function get_age($birthDate_old) {
$birthDate = ($birthDate_old);
return date_diff(new DateTime($birthDate), new DateTime('today'))->y;
}
$birthDate = "26/01/1958";
$birthDate = preg_replace("(/)", "-", $birthDate);
echo get_age($birthDate);
希望对您有所帮助!让我知道