Parse and create ISO 8601 Date and time intervals, like PT15M in PHP

thep3300

New Member
A library and webservice I am using, communicates time-intervals in ISO 8601: PnYnMnDTnHnMnS. I want to convert such formats to seconds. And vice versa. Seconds are a lot easier to calculate with.Example interval values are:
  • PT1M or PT60S (1 minute)
  • PT1H, PT60M or PT3600S (1 hour)
I need two functions: parse from such values to seconds: \[code\]iso8601_interval_to_seconds()\[/code\] and from seconds into such intervals: \[code\]iso8601_interval_from_seconds()\[/code\].The latter is rather simple, because it could be done as `"PT{$seconds}S", just pass seconds along, at all times. Maybe this can be done nicer with a parser that switches to H(hour) or M(minute)? The first is harder, but maybe there is a trick with one of the many string-to-date converters in PHP? I would love to learn how to use such a function for parsing intervals. Or learn an alternative.
 
Back
Top