DateTime格式要求参数1为字符串,Object给定-Symfony2


DateTime format expects parameter 1 to be string, Object given - Symfony2

我在Symfony2中使用格式化函数时遇到了一些问题,因为我试图将日期间隔插入表中,以便为创建的发票设置到期日期。

这是我所拥有的:

$today = new 'DateTime();
$interval = $today->add(new 'DateInterval('P1M'));
$invoice->setDueDate($interval->format('Y-m-d H:i:s'));

然而,当我将鼠标悬停在PHPStorm中的format参数上时,它告诉我它需要的是一个DateTime对象,而不是字符串,我在我的探查器中得到以下错误:

错误:在字符串上调用成员函数format()

因此,我将行改为:

$invoice->setDueDate($interval->format(new 'DateTime()));

但当我运行它时,我的探查器会给出以下错误:

警告:DateTime::format()要求参数1为字符串,对象给定

这几乎像是一个陷阱22的情况!我真的很困惑,我是使用字符串还是DateTime对象,因为其中一个失败了,但却警告我需要使用其中一个。。

有什么想法吗?

我猜$invoice->setDueDate()就是需要DateTime实例的那个。因此该行应为$invoice->setDueDate($interval);