我正在使用以下函数来计算人们的年龄(以年为单位),从生日日期(欧洲格式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
正常工作的数据示例: $age = get_age($birthday);
对于$birthday值 = 05/04/1946,它工作正常,但对于$birthday值 = 26/01/1958,我得到上面的错误。我不明白为什么数据在我看来在2种情况下的格式相同。
你有什么线索吗?
谢谢。问候。
杨__羊羊
红颜莎娜
达令说
收到一只叮咚
随时随地看视频慕课网APP