An easy approach would be to calculate the minutes between the trimmed and actual time, then round that to multiples of 30 and add it again to the trimmedTime.
Should be a simple calculation like below (not tested, though)
$time
$trimmedTime = trimToHours($time)
$minutes = minutesBetween( $trimmedTime, $time)
$roundedMinutes= round( $minutes : 30 ) * 30
$roundedTime = addMinutes( $trimmedTime, $roundedMinutes)
regards, Fabian