If I have a time format string like \"14:30:00\" (\"hours:minutes:seconds\"), how do I get a DateInterval from the string?
I can get a DateTime:
If you want an interval that is 14 hours and 30 minutes, simply use the constructor...
$interval = new DateInterval('PT14H30M');
To break it down...
P
- all interval spec strings must start with P
(for Period). We aren't using any period intervals though so on to...T
- this starts the Time spec14H
- 14 hours30M
- 30 minutesIf you must use the string 14:30:00, I'd parse it with sscanf
and use the parts...
list($hours, $minutes, $seconds) = sscanf('14:30:00', '%d:%d:%d');
$interval = new DateInterval(sprintf('PT%dH%dM%dS', $hours, $minutes, $seconds));