Search code examples
phpsymfonydoctrine-orm

Symfony 6.0 time format problem database upload


There were some problems with symfony over time. unfortunately, this is not the first time I have run into this. Has anyone ever encountered this problem? If so, thank you for your help.

Controller.php

  if($xml = simplexml_load_file($feedUrl)) {
            $result["chanel"] = $xml->xpath("/rss/channel/item");

            foreach($result as $key => $attribute) {
                $i=0;

                foreach($attribute as $element) {


                    $ret[$i]['title'] = (string)$element->title;
                    $ret[$i]['category'] = (string)$element->category;
                    $ret[$i]['link'] = (string)$element->link;
                    $ret[$i]['pubDate'] = json_decode(json_encode($element->pubDate), TRUE);
                    $ret[$i]['enclosure'] = (array)$element->enclosure;
                    $ret[$i]['description'] = (string)$element->description;

                    $i++;
                }
            }
        }

        foreach ($ret as $feed){

        $newnews = new Newsfeed();

        $newnews->setTitle($feed['title']);
        $newnews->setCategory($feed['category']);
        $newnews->setLink($feed['link']);
        $newnews->setDescription($feed['description']);
        $newnews->setDate(date("Y-m-d h:i:sa", strtotime($feed['pubDate'][0])));
        $newnews->setImage($feed['enclosure']['@attributes']['url']);
        $newnews->setSource('2');

            $entityManager->persist($newnews);
            $entityManager->flush();


        }

This problem

enter image description here


Solution

  • The date() function returns a string, so this line won't work:

    $newnews->setDate(date("Y-m-d h:i:sa", strtotime($feed['pubDate'][0])));
    

    You can probably just use:

    $newnews->setDate(new \DateTimeImmutable($feed['pubDate'][0]));