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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码