public function getDatetime($data, $timezone = null)
    {
        if ($data instanceof \DateTime) {
            return $data;
        }

        if ($data instanceof \DateTimeImmutable) {
            return \DateTime::createFromFormat(\DateTime::ATOM, $data->format(\DateTime::ATOM));
        }

        // the format method accept array or integer
        if (is_numeric($data)) {
            $data = (int) $data;
        }

        if (\is_string($data)) {
            $data = strtotime($data);
        }

        $date = new \DateTime();
        $date->setTimestamp($data);
        $date->setTimezone(new \DateTimeZone($timezone ?: $this->timezoneDetector->getTimezone()));

        return $date;
    }
public function getDatetime($data, $timezone = null)
    {
        if ($data instanceof \DateTime) {
            return $data;
        }

        if ($data instanceof \DateTimeImmutable) {
            return \DateTime::createFromFormat(\DateTime::ATOM, $data->format(\DateTime::ATOM));
        }

        // the format method accept array or integer
        if (is_numeric($data)) {
            $data = (int) $data;
        }

        if (\is_string($data)) {
            $data = strtotime($data);
        }

        $date = new \DateTime();
        $date->setTimestamp($data);
        $date->setTimezone(new \DateTimeZone($timezone ?: $this->timezoneDetector->getTimezone()));

        return $date;
    }
public function getDatetime ( $ data , $ timezone = null ) { if ( $ data instanceof \ DateTime ) { return $ data ; } if ( $ data instanceof \ DateTimeImmutable ) { return \ DateTime :: createFromFormat ( \ DateTime :: ATOM , $ data -> format ( \ DateTime :: ATOM ) ) ; } // the format method accept array or integer if ( is_numeric ( $ data ) ) { $ data = ( int ) $ data ; } if ( \ is_string ( $ data ) ) { $ data = strtotime ( $ data ) ; } $ date = new \ DateTime ( ) ; $ date -> setTimestamp ( $ data ) ; $ date -> setTimezone ( new \ DateTimeZone ( $ timezone ? : $ this -> timezoneDetector -> getTimezone ( ) ) ) ; return $ date ; }
Gets a date time instance by a given data and timezone.

@param \DateTime|\DateTimeInterface|string|int $data     Value representing date
@param string|null                             $timezone Timezone of the date

@return \DateTime
Gets a date time instance by a given data and timezone .