protected function convertTimeZone(DateTimeInterface $date, $timezone)
    {
        $timezone = ($timezone instanceof DateTimeZone) ? $timezone : new DateTimeZone($timezone);

        if ($date->getTimezone() !== $timezone) {
            if ($date instanceof DateTime) {
                // DateTime is not immutable, so make a copy
                $date = (clone $date);
                $date->setTimezone($timezone);
            } elseif ($date instanceof DateTimeImmutable) {
                $date = $date->setTimezone($timezone);
            }
        }

        return $date;
    }
protected function convertTimeZone(DateTimeInterface $date, $timezone)
    {
        $timezone = ($timezone instanceof DateTimeZone) ? $timezone : new DateTimeZone($timezone);

        if ($date->getTimezone() !== $timezone) {
            if ($date instanceof DateTime) {
                // DateTime is not immutable, so make a copy
                $date = (clone $date);
                $date->setTimezone($timezone);
            } elseif ($date instanceof DateTimeImmutable) {
                $date = $date->setTimezone($timezone);
            }
        }

        return $date;
    }
protected function convertTimeZone ( DateTimeInterface $ date , $ timezone ) { $ timezone = ( $ timezone instanceof DateTimeZone ) ? $ timezone : new DateTimeZone ( $ timezone ) ; if ( $ date -> getTimezone ( ) !== $ timezone ) { if ( $ date instanceof DateTime ) { // DateTime is not immutable, so make a copy $ date = ( clone $ date ) ; $ date -> setTimezone ( $ timezone ) ; } elseif ( $ date instanceof DateTimeImmutable ) { $ date = $ date -> setTimezone ( $ timezone ) ; } } return $ date ; }
Given a date object, return a new date object with the specified timezone.
This method ensures that the original date object is not mutated.

@param DateTimeInterface   $date
@param DateTimeZone|string $timezone

@return DateTimeInterface|DateTime|DateTimeImmutable
Given a date object return a new date object with the specified timezone . This method ensures that the original date object is not mutated .