首页 > 解决方案 > 如何通过 formbuilder 将日期存储到我的数据库中?

问题描述

我尝试在我的数据库中存储一个日期:

$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;
  }

标签: phpsymfonydatetimedoctrineentity

解决方案


您的格式和日期时间不正确。使用以下代码

$date_time = DateTime::createFromFormat('Y-m-d\TH:i:sP', "2005-08-15T15:52:01+00:00");
$entity->setTimestamp($date_time->format("d.m.Y"));

这也将是简单的字符串。setTimestamp因此,对您的功能进行更改。否则你可以返回整个$date_time.


推荐阅读