php - 如何通过 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;
}
解决方案
您的格式和日期时间不正确。使用以下代码
$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
.
推荐阅读
- c++ - 绘制简单多边形时出现 Qt C++ 错误
- mariadb - 在 MariaDB 10.3 中允许对特定用户进行外部访问
- java - kafkaProducer第一次尝试发送记录时无法创建主题INVALID_REPLICATION_FACTOR
- java - 由于快照依赖关系(?),Gradle 同步失败(Android Studio)
- json - 以角度显示 json 数据(使用打字稿)
- xslt-1.0 - 基于 xsl 1.0 中的一个值对相似记录进行分组
- python - Tensorflow:tf.control_dependencies 没有按预期工作
- javascript - 如何基于一个属性对数组进行排序,然后在 React Redux 应用程序中按另一个属性分组
- javascript - 如何从 html 页面中选择某些文本的位置获取 div 的 id
- r - 从使用 HH 包中的 likertplot 函数创建的堆积条形图中删除 NA