How to normalize datetime with Serializer in Symfony5

I’m tring to export data in csv with Serializer. I’ve a datetime field in my table. How can I convert this date in d/m/Y ?

    public function csvExport(ComplaintRepository $repo)
        $serializer = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]);

        $complaints = $repo->findAll();
        $list = $serializer->serialize($complaints, 'csv', [AbstractNormalizer::IGNORED_ATTRIBUTES => ['id', 'registrationdate', 'gdpr', 'langvis', 'department', 'status', 'slug']]);

        echo $list;        

        $response = new Response();
        $response->headers->set('Content-Type', 'text/csv');
        //it's gonna output in a testing.csv file
        $response->headers->set('Content-Disposition', 'attachment; filename="test.csv"');

        return $response;

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *