"DateTime::__construct(): Failed to parse time string (@)" 函数中的致命错误
"DateTime::__construct(): Failed to parse time string (@)" Fatal eror in function
我用函数将时间转换为时间,但出现一个我不完全理解的错误。
函数:
function secondsToTime($seconds)
{
$dtF = new DateTime("@0");
$dtT = new DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a Days, %h H %i M');
// return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
错误:
PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed
to parse time string (@) at position 0 (@): Unexpected character in
/home/user/public_html/Config.php:479 Stack
trace:
#0 /home/user/public_html/Config.php(479): DateTime->__construct('@')
#1 /home/user/public_html/index.php(56):
secondsToTime('')
#2 {main} thrown in /home/user/public_html/Config.php on line 479
导致此错误的原因是什么?
您正在调用您的函数而没有传递任何秒数。 new DateTime("@")
确实是一个错误,因为 UNIX 时间戳格式至少需要一位数字。
考虑添加类型提示 function secondsToTime(int $seconds)
,因为这将确保您确实拥有一个数字。
我用函数将时间转换为时间,但出现一个我不完全理解的错误。
函数:
function secondsToTime($seconds)
{
$dtF = new DateTime("@0");
$dtT = new DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a Days, %h H %i M');
// return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
错误:
PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed
to parse time string (@) at position 0 (@): Unexpected character in
/home/user/public_html/Config.php:479 Stack
trace:
#0 /home/user/public_html/Config.php(479): DateTime->__construct('@')
#1 /home/user/public_html/index.php(56):
secondsToTime('')
#2 {main} thrown in /home/user/public_html/Config.php on line 479
导致此错误的原因是什么?
您正在调用您的函数而没有传递任何秒数。 new DateTime("@")
确实是一个错误,因为 UNIX 时间戳格式至少需要一位数字。
考虑添加类型提示 function secondsToTime(int $seconds)
,因为这将确保您确实拥有一个数字。