You have multiple options. The most comprehensive one would be to save the date together with the time, then the calculation is straightforward.
If you insist of having only the time and the end time is always later on the same day or on the next day, you just need to check if the difference would be negative (if starttime > endtime) and then either add 24, i.e. (endtime - starttime) + 24 or - if your functions or methods do not like negative values - shift (add) the endtime by the difference of 24 - starttime, which is always positive and then the endtime is your period.