Call to a member function format() on a non-object Symfony datetime error

When you assign time for datetime in you Entity in Symfony project, you might get this problem. The fix is simple.
If you have your entity column setup as Date or datetime like

@ORMColumn(name="date_created", type="datetime", nullable=false)

The respective setDateCreated() method expects standard datetime object.
So,

$entity->setDateCreated(new DateTime());

will solve the problem. If you want to give different time other than today’s date, then you can populate DateTime object and provide that