我有一个Symfony实体Equipment()。这个实体代表了一个设备..。它具有以天为单位的控件和控制周期(例如,在每个控件之前需要30天)。控件有一个日期,我正在尝试显示下一个所需的控制日期。
这是我的设备实体:
<?php
namespace StockBundle\Entity;
use StockBundle\Entity\EquipmentControle;
/**
* Equipment
*
* @ORM\Table(name="stock_equipment")
* @ORM\Entity(repositoryClass="StockBundle\Repository\EquipmentRepository")
*/
class Equipment
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="EquipmentControle", mappedBy="equipment")
*/
private $controles;
/**
* @var float
*
* @ORM\Column(name="control_interval", type="integer", options={"default" : 0})
*/
private $controlInterval = 0;
public function getControles()
{
return $this->controles;
}
public function getControlInterval()
{
return $this->controlInterval;
}
public function setControles($controles)
{
$this->controles = $controles;
}
public function setControlInterval($controlInterval)
{
$this->controlInterval = $controlInterval;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
public function getLastControlDate(){
$lastControle = null;
foreach ($this->getControles() as $c){
if($lastControle == null || $c->getDate() > $lastControle->getDate()){
$lastControle = $c;
}
}
return $lastControle == null ? null : $lastControle->getDate();
}
public function getNextControlDate(){
if($this->getLastControlDate() == null && $this->getControlInterval() > 0){
return new \DateTime("NOW");
}elseif ($this->getLastControlDate() != null && $this->getControlInterval() > 0){
return $this->getLastControlDate()->modify('+'.$this->getControlInterval().'day');
}else{
return null;
}
}
}我使用小枝显示我的上一个和下一个控件日期:
{% if e.lastControlDate is not null %}{{ e.lastControlDate|date('d/m/Y') }}{% else %}-{% endif %}
{% if e.nextControlDate is not null %}{{ e.nextControlDate|date('d/m/Y') }}{% else %}-{% endif %}但nextControlDate似乎总是比设想的日期多两倍。
我最后一次控制在2018年11月29日和$controlInterval on 10,得到19/12/2018年的下一个控制。当我试图更改controlInterval时,情况也是一样的。
当我在nextControlDate之前显示lastControlDate时,控制lastControl和nextControl的日期相同.
有什么帮助吗?
发布于 2018-11-30 08:48:29
使用日期时间的不可变版本,这样您就可以多次使用它,而无需从上次修改中重新计算。
if ($this->getLastControlDate() != null && $this->getControlInterval() > 0){
$dateImmuable = new \DateTimeImmutable();
$dateImmuable->createFromMutable($this->getLastControlDate());
return $dateImmuable->modify('+'.$this->getControlInterval().'day');
} https://stackoverflow.com/questions/53545614
复制相似问题