Bernd Jendrissek Bernd Jendrissek - 1 year ago 32
PHP Question

How to format a time interval concisely as ISO 8601 start/end in PHP?

I want to represent a time interval as start/end but also while taking advantage of the abbreviations that ISO 8601 seems to allow: for interval end fields that are the same as in the starting point. For example, the interval starting right now and lasting a week would be 2016-06-08T12:27Z/15T12:27 (note the omission of a second "2016-06-").

To ward of potential XY-problem second-guessing: I don't want this interval represented as 2016-06-08T12:27Z/P1W, even if it is shorter. I do want the end time represented directly, but abbreviated to the extent possible.

I don't think DatePeriod does what I want - does it? It seems to either do half the job (start and end independently, with no possibility for abbreviation) or the wrong job (returning a duration, not an end time).

Answer Source

I don't understand your problem. But what about short record of period:

Make string 2016-06-08T12:27Z/2016-06-15T12:27

and by simple regex reduce it:

^(.+)\b(.+)\/\1(.+)  to  \1\2/\3  =>  2016-06-08T12:27Z/15T12:27 



Common result after testing