【问题标题】:how can i get only the date from a dateTime entity?如何仅从 dateTime 实体中获取日期?
【发布时间】:2020-05-05 23:04:24
【问题描述】:

当我想获取列的日期时,它会显示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;
}

任何帮助都会受到赞赏:)

【问题讨论】:

标签: api postman symfony4


【解决方案1】:

您的方法“getDateEcheance”返回一个 DateTime 对象和 $this->dateEcheance->format('d/m/Y');在这里不相关。

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

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

    return '';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多