尝试使用时间戳创建新的DateTime对象时出现此构造错误:
异常:DateTime :: _ construct():无法解析位置8(8)处的时间字符串(1372622987):DateTime-> _construct()中的意外字符
对象创建代码是:
$start_date = new DateTime( "@{$dbResult->db_timestamp}" );
Run Code Online (Sandbox Code Playgroud)
其中$ dbResult-> db_timestamp是从数据库中获取的有效unix时间戳.有问题的时间戳是:
1372622987
我会理解传递无效格式的错误,但这是一个真正的时间戳.
这是非常奇怪的原因:我自从运行一个脚本来创建一个新的DateTime对象,其中时间戳作为硬编码值传入,并且它报告没有错误.
这似乎是一次性的,但我需要一个解释,如果有一个,因为我无法承受再次发生这种情况.
saa*_*rim 75
如果你硬编码,你应该使用setTimestamp:
$start_date = new DateTime();
$start_date->setTimestamp(1372622987);
Run Code Online (Sandbox Code Playgroud)
在你的情况下
$start_date = new DateTime();
$start_date->setTimestamp($dbResult->db_timestamp);
Run Code Online (Sandbox Code Playgroud)
Aba*_*her 11
将您的代码更改为此
$start_date = new DateTime( "@" . $dbResult->db_timestamp );
Run Code Online (Sandbox Code Playgroud)
它会工作正常