gpt4 book ai didi

PHP DateTime __construct() 无法解析位置 x 处的时间字符串 (xxxxxxxx)

转载 作者:IT王子 更新时间:2023-10-29 00:00:46 24 4
gpt4 key购买 nike

我在尝试使用时间戳创建新的 DateTime 对象时遇到了这个构造错误:

Exception: DateTime::_construct(): Failed to parse time string (1372622987) at position 8 (8): Unexpected character in DateTime->_construct()

对象创建代码为:

$start_date = new DateTime( "@{$dbResult->db_timestamp}" );

其中 $dbResult->db_timestamp 是从数据库中获取的有效 unix 时间戳。有问题的时间戳是:

1372622987

如果传递的格式无效,我会理解这个错误,但这是一个真正的时间戳。

这很奇怪的原因是:我运行了一个脚本来创建一个新的 DateTime 对象,并将时间戳作为硬编码值传入,并且没有报告任何错误。

这似乎是一次性的,但如果有的话,我需要一个解释,因为我不能再发生这种情况。

最佳答案

如果你硬编码,你应该使用 setTimestamp:

$start_date = new DateTime();
$start_date->setTimestamp(1372622987);

在你的情况下

$start_date = new DateTime();
$start_date->setTimestamp($dbResult->db_timestamp);

关于PHP DateTime __construct() 无法解析位置 x 处的时间字符串 (xxxxxxxx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17427503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com