如何在学说中加2小时?

dea*_*ase 1 php doctrine-orm

使用以下代码,

protected $token;
 /** @Column(name="assigneddate", type="datetime", columnDefinition="datetime") */
private $assigneddate;


/** @Column(name="expirydate", type="datetime", columnDefinition="datetime") */
private $expirydate;

/** @PreUpdate */
public function updated()
{
    //$this->assigneddate = new \DateTime("now");
}
public function __construct()
{

    $this->expirydate = $this->expirydate = new \DateTime("now");
    $this->assigneddate = $this->assigneddate = new \DateTime("now");
Run Code Online (Sandbox Code Playgroud)

}

我如何添加2小时?

fak*_*ken 7

这更像是一个PHP问题.要向DateTime PHP对象添加时间,请使用add方法,该方法接受DateInterval对象.在您的情况下,如果您想在到期日期间增加2小时:

// Create DateTime object with current time/date
$this->expirydate = new \DateTime("now");
// Add two hours
$this->expirydate->add(new \DateInterval("PT2H"));
Run Code Online (Sandbox Code Playgroud)

其中"PT2H"表示"2小时的周期时间",如此处所述.