Api 如何仅从dateTime实体获取日期?

Api 如何仅从dateTime实体获取日期?,api,postman,symfony4,Api,Postman,Symfony4,当我想得到一列的日期时,它会显示给我。 以下是包含dateTime属性的实体的代码: /** * @ORM\Column(type="datetime") */ private $dateEcheance; public function getDateEcheance(): ?\DateTimeInterface { $this->dateEcheance->format('d/m/Y'); return $this->dateEcheance; }

当我想得到一列的日期时,它会显示给我。 以下是包含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;
}
欢迎提供任何帮助:)

您的方法“GetDateCeance”返回一个DateTime对象和$this->DateCeance->format('d/m/Y');这与此无关

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

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

    return '';
}
方法“getDateCeance”返回一个DateTime对象和$this->DateCeance->format('d/m/Y');这与此无关

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

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

    return '';
}

您正在使用默认的规范化器。以和的发布为例,您使用的是默认的规范化程序。请查看和发布的示例。