public static function convertTimezone(
        $date,
        string $toTimezone,
        string $fromTimezone = "UTC",
        bool $immutable = false
    ): \DateTimeInterface
    {
        if ($date instanceof \DateTimeInterface) {
            $date = $date->format(self::FORMAT);
        } elseif (!is_string($date)) {
            throw new \InvalidArgumentException(
                "invalid value provided for 'date'; expecting an instance of ".
                "DateTimeInterface or a date as string"
            );
        }

        $from = new \DateTimeZone($fromTimezone);
        $to = new \DateTimeZone($toTimezone);

        $ret = ($immutable)
            ? new \DateTimeImmutable($date, $from)
            : new \DateTime($date, $from);

        return $ret->setTimezone($to);
    }
public static function convertTimezone(
        $date,
        string $toTimezone,
        string $fromTimezone = "UTC",
        bool $immutable = false
    ): \DateTimeInterface
    {
        if ($date instanceof \DateTimeInterface) {
            $date = $date->format(self::FORMAT);
        } elseif (!is_string($date)) {
            throw new \InvalidArgumentException(
                "invalid value provided for 'date'; expecting an instance of ".
                "DateTimeInterface or a date as string"
            );
        }

        $from = new \DateTimeZone($fromTimezone);
        $to = new \DateTimeZone($toTimezone);

        $ret = ($immutable)
            ? new \DateTimeImmutable($date, $from)
            : new \DateTime($date, $from);

        return $ret->setTimezone($to);
    }
public static function convertTimezone ( $ date , string $ toTimezone , string $ fromTimezone = "UTC" , bool $ immutable = false ) : \ DateTimeInterface { if ( $ date instanceof \ DateTimeInterface ) { $ date = $ date -> format ( self :: FORMAT ) ; } elseif ( ! is_string ( $ date ) ) { throw new \ InvalidArgumentException ( "invalid value provided for 'date'; expecting an instance of " . "DateTimeInterface or a date as string" ) ; } $ from = new \ DateTimeZone ( $ fromTimezone ) ; $ to = new \ DateTimeZone ( $ toTimezone ) ; $ ret = ( $ immutable ) ? new \ DateTimeImmutable ( $ date , $ from ) : new \ DateTime ( $ date , $ from ) ; return $ ret -> setTimezone ( $ to ) ; }
Convert a date to another timezone

@param $date The date to convert
@param string $toTimezone The timezone to conver to
@param string $fromTimezone The timezone to convert from
@param bool $immutable Whether to return an immuateable instance
@return \DateTimeInterface
Convert a date to another timezone