【发布时间】:2019-09-25 10:08:52
【问题描述】:
我尝试在我的数据库中存储一个日期:
$entity->setTimestamp(\DateTime::createFromFormat('d.m.Y', "2005-08-15T15:52:01+00:00"));
但我收到错误消息:
传递给 App\Entity\Documents::setTimestamp() 的参数 1 必须 实现接口 DateTimeInterface,给定布尔值,
我的实体中的功能:
public function setTimestamp(\DateTimeInterface $timestamp): self {
$this->timestamp = $timestamp;
return $this;
}
【问题讨论】:
-
\DateTime::createFromFormat()失败时返回布尔值 FALSE。您的格式字符串与日期字符串不匹配。 php.net/manual/en/datetime.createfromformat.php -
正确答案是
$entity->setTimestamp(\DateTime::createFromFormat('d.m.Y', "15.08.2005"));
标签: php symfony datetime doctrine entity