I assume, your wanted output is 0.5
- half an hour. If you want 0.30
you just can replace the :
in your format string with a dot .
.
You can get the interval in seconds by subtracting the time-stamps of start and end time:
$diff_s = $end->getTimestamp() - $start->getTimestamp();
And then just divide by 3600 to get the decimal hours:
$diff_h = $diff_s/3600;
echo round($diff_h, 2);
See working example on ideone.com.
Summarizing a discussion in the comments:
Of course time zone and daylight saving shifts are always an issue, when you work with Dates and Times. If (!) you consider the right time zones in construction of your DateTime
objects, the timestamp diff in this answer will show you the seconds really(*) passed in between. And this may differ from the time seemingly has passed on your wall clock.
(*) The time really passed? No, unfortunately not. When it comes to leap seconds, the difference of two Unix timestamps is not necessarily the number of seconds passed. At least, this error by its nature is relatively small. See here for more info on that: Unix time and leap seconds