Michael Emerson
Michael Emerson

Reputation: 1813

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

I'm having some troubles using the format function in Symfony2 when trying to insert a date interval into a table, for the purpose of setting due dates for created invoices.

Here is what I have:

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

However, when I hover over the format parameter in PHPStorm, it tells me that it's expecting a DateTime object not a string, and I get the following error in my profiler:

Error: Call to a member function format() on string

So, I changed the line to this:

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

But when I run that, my profiler gives this error:

Warning: DateTime::format() expects parameter 1 to be string, object given

It almost seems like a catch 22 situation! I am really baffled, do I use a string or a DateTime object, because either one fails yet warns me I need to use one or the other..

Any ideas?

Upvotes: 1

Views: 4078

Answers (1)

hanzi
hanzi

Reputation: 2987

I’d guess that $invoice->setDueDate() is the one expecting a DateTime instance. So the line should be $invoice->setDueDate($interval);

Upvotes: 1

Related Questions