I have arrays of dates in Y-m-d
format that may be any combination of ten set dates that are zero, one, or more days apart.
e.g. Here is a possible full set:
[
'2011-01-01',
'2011-01-02',
'2011-01-03',
'2011-01-04',
'2011-01-05',
'2011-01-06',
'2011-01-07',
'2011-01-08',
'2011-01-09',
'2011-01-10'
]
The arrays that are created from that set can be any combination of consecutive or gapped dates.
I currently have them printing pretty much as they're returned. e.g. here's another possible data set:
[
'2011-01-02',
'2011-01-03',
'2011-01-04',
'2011-01-08',
]
(What's actually printed is more like "Friday, Jan. 2…", but we'll stick with the simple date string.)
I'd like to condense it so that if there are three or more consecutive days, those become a range, e.g the above example would become:
[
'2011-01-02 to 2011-01-04',
'2011-01-08',
]
which would eventually become:
[
'Sunday, Jan. 2 - Tuesday, Jan. 4',
'Saturday Jan. 8',
]
Is there a way to loop through and consolidate consecutive dates as ranged date strings?