首页 > 解决方案 > 如何从 PHP 中的 DateTimeInterface 创建 DateTime?

问题描述

这是一个相当简单的问题,但我在互联网上找不到真正的答案。

所以假设,在 PHP 中,我有一个 a 的实例DateTimeInterface,并且我需要 aDateTime在同一时间。这样做的首选方法是什么?通过字符串进行转换似乎相当复杂,我不确定要使用哪种格式。也许通过时间戳?我不确定在涉及时区时这是否有效。

标签: phpdatetime

解决方案


在 PHP 8 中:createFromInterface

$dateTime = DateTime::createFromInterface($dateTimeInterface);

推荐阅读