I have an entity with this field:
/**
* @var \DateTime
*
* @Groups({"list", "details"})
* @ORM\Column(name="timestamp", type="datetime")
*/
private $timestamp;
And I have the following code:
$date = new \DateTime();
$myentity->setTimestamp($date); //Fails
$myentity->setTimestamp($date->getTimestamp()); //Also fails
I get the following error:
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Object of class DateTime could not be converted to int
What is the proper way to store a datetime field with symfony and doctrine?
There is possible than the oriented object method : $date = new \DateTime(); $myentity->setTimestamp($date->getTimestamp());
will be override by ORM.
If you attempt with the procedural style : $date = date_create(); $myentity->setTimestamp(date_timestamp_get($date));
have you any satisfied result ?
See DateTime::format
You can also attempt with $date = new DateTime(); $myentity->setTimestamp($date->format('U')); // return seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
See time()