Create date ranges from consecutive dates in an array
I have arrays of dates Y-m-d format that may be any combination of ten set dates that are one day 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 dates— all of them, one of them, some consecutive, all consecutive, etc.
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 check the time difference, create a start time and end time for the range(s), and then gather up the stragglers?
- 1.2k
- 2
- 15
- 22