Skip to content
Advertisement

Symfony – set entity field as timestamp

In my Symofny project I want for my entity to have timestamp field.

/**
 * @ORMColumn(type="datetime", nullable=false)
 */
private $timestamp;

/**
 * @ORMPreUpdate
 * @throws Exception
 */
public function setTimestamp()
{
    $this->timestamp = new DateTime("now");
    return $this;
}

I want to be saved in timestamp format? How can I accomplish that? Like // => 1387909800

I am on Symfony 4.3 version.

Advertisement

Answer

Try getTimestamp();

/**
 * @ORMColumn(type="datetime", nullable=false)
 */
private $timestamp;

/**
 * @ORMPreUpdate
 * @throws Exception
 */
public function setTimestamp()
{
    $date = new DateTime();
    $this->timestamp = $date->getTimestamp();
    return $this;
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement