You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

秒转天时分功能在部署服务器正常,XAMPP开发环境报错求助

问题排查与解决方案

问题根源

错误提示显示Failed to parse time string (@),说明$seconds变量为空(未定义、为空字符串或非有效数值),导致拼接后的时间字符串为@,不符合DateTime构造函数的Unix时间戳格式要求(正确格式应为@[数字],比如@0)。

修复方案

1. 校验并确保$seconds为有效数值

在使用$seconds前,先做合法性校验,确保它是有效的整数:

// 确保$seconds是有效的非负整数,默认设为0
$seconds = is_numeric($seconds) ? (int)$seconds : 0;

$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes');

2. 替代实现(不依赖DateTime类)

如果不想依赖DateTime类,也可以用数学计算直接完成转换:

$days = floor($seconds / 86400);
$remaining = $seconds % 86400;
$hours = floor($remaining / 3600);
$minutes = floor(($remaining % 3600) / 60);

return "$days days, $hours hours, $minutes minutes";

额外说明

部署服务器运行正常,说明生产环境中$seconds始终是有效的数值;本地开发环境触发错误,大概率是测试时$seconds未被正确赋值(比如未传入该变量)。

内容的提问来源于stack exchange,提问作者Hayden O'Sullivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 12:50:22