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

问题描述 投票:0回答:1

当我想获取一列的日期时,它显示的是 这个 .这是我为包含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;
}

任何帮助将是apprecaited:)

api postman symfony4
1个回答
0
投票

你的方法 "getDateEcheance "返回一个DateTime对象,并且$this->dateEcheance->format('dmY');在这里不相关。

也许你需要创建另一个类似的方法。

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

    return '';
}
© www.soinside.com 2019 - 2024. All rights reserved.