如何通过formbuilder将日期存储到数据库中?

问题描述 投票:0回答:1

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

$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;
  }
php symfony datetime doctrine entity
1个回答
0
投票

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

$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

© www.soinside.com 2019 - 2024. All rights reserved.