我怎样才能只从 dateTime 实体中获取日期?

how can i get only the date from a dateTime entity?

当我想获取列的日期时,它会显示 this。 这是包含 dateTime 属性的实体的代码:

/**
 * @ORM\Column(type="datetime")
 */
private $dateEcheance;

public function getDateEcheance(): ?\DateTimeInterface
{
    $this->dateEcheance->format('d/m/Y');
    return $this->dateEcheance;
}

public function setDateEcheance(\DateTimeInterface $dateEcheance): self
{
    date_default_timezone_set('Africa/Tunis');
    $this->dateEcheance = $dateEcheance;

    return $this;
}

这是我的API:

/**
 * @Route("/api/getContract", name="getContract", methods={"GET"})
*/
public function getContract(ContratRepository $contratRepo) 
{    
    $contrat= $contratRepo->findAll();

    $encoders = [new JsonEncoder()];
    $normalizers =[new ObjectNormalizer()];
    $serializer =new Serializer($normalizers, $encoders);
    $jsonContent =$serializer->serialize($contrat, 'json', ['circular_reference_handler' => function($object){
        return $object->getId();
    }]);

    $response = new Response($jsonContent);

    $response->headers->set('Content-Type', 'application/json');
    $response->headers->set('Access-Control-Allow-Origin', '*');

    return $response;
}

如有任何帮助,我们将不胜感激:)

你的方法 "getDateEcheance" return 一个 DateTime 对象和 $this->dateEcheance->format('d/m/Y');此处不相关。

也许您需要创建另一个类似的方法:

public function getDateEcheanceLibelle(): ?String
{
    if( isset($this->dateEcheance) )
        return $this->dateEcheance->format('d/m/Y');

    return '';
}