dongyinglan8707 2018-08-10 18:41
浏览 719
已采纳

“DateTime :: __ construct():无法解析时间字符串(@)”函数中的致命错误

I turn time into time with function but I'm getting an error that I do not fully understand.

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');
    }

Error:

 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

what could be the cause of this error?

  • 写回答

1条回答 默认 最新

  • duanba4942 2018-08-10 18:53
    关注

    You are calling your function without passing in any amount of seconds. new DateTime("@") is indeed an error, as the UNIX Timestamp format requires at least one digit.

    Consider adding the typehint function secondsToTime(int $seconds) as this will ensure you actually have a number.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统