You could try this one.
For the timezone, you code get from
\Magento\Framework\Stdlib\DateTime\TimezoneInterface::getDefaultTimezone
or
\Magento\Framework\Stdlib\DateTime\TimezoneInterface::getConfigTimezone
class DateTime
{
static function convertDateTimeFormat(
string $originalDatetime,
string $sourceFormat = 'Y-m-d H:i:s',
string $destinationFormat = 'Y-m-d H:i:s',
string $sourceTimezone = 'UTC',
string $destinationTimezone = null
) {
$sourceTimezoneObj = new \DateTimeZone($sourceTimezone);
$destinationTimezone = new \DateTimeZone($destinationTimezone ?? $sourceTimezone);
$dateObject = \Datetime::createFromFormat($sourceFormat, $originalDatetime, $sourceTimezoneObj);
if ($dateObject === false) {
throw new \InvalidArgumentException('Please verify your input');
}
return $dateObject->setTimezone($destinationTimezone)->format($destinationFormat);
}
}