PHP DateTime __construct()无法在x位置解析时间字符串(xxxxxxxx) [英] PHP DateTime __construct() Failed to parse time string (xxxxxxxx) at position x

查看:1887
本文介绍了PHP DateTime __construct()无法在x位置解析时间字符串(xxxxxxxx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had this construction error when trying to creating a new DateTime object using a timestamp:


异常:DateTime :: _ > construct():无法解析位置8(8)处的时间字符串(1372622987):DateTime中的意外字符 - > _construct()

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时间戳。时间戳是:

Where $dbResult->db_timestamp is a valid unix timestamp taken from a database. The timestamp in question was:


1372622987

1372622987

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

I would understand this error for invalid formats being passed, but this is a genuine timestamp.

这是非常奇怪的原因:我运行一个脚本来创建一个新的DateTime对象以时间戳传递为硬编码值,并且没有报告错误。

The reason this is very strange: I since ran a script to create a new DateTime object with the timestamp passed in as a hard coded value, and it reported no errors.

这似乎是一个关闭,但是我需要一个解释有一个,因为我买不起这个再次发生。

This seems to have been a one off, but I need an explanation if there is one, as I can't afford for this to happen again.

推荐答案

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

You should use setTimestamp instead, if you hardcode it:

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

在您的情况下

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

这篇关于PHP DateTime __construct()无法在x位置解析时间字符串(xxxxxxxx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆