I have an array like:
array(
['march'] => array(
'2', '3', '4', '5', '6', '23', '24', '25', '30'
);
);
I need to convert that into:
03/02/2012 - 03/06/2012
03/23/2012 - 03/25/2012
03/30/2012
You can find the consecutive timespans by looping through your array like so:
$months = array(
'march' => array(
'2', '3', '4', '5', '6', '23', '24', '25', '30'
)
);
$current_day = 0;
$span_start = 0;
$span_end = 0;
foreach($months['march'] as $day) {
if ($day > ++$current_day) {
$current_day = $day;
if ($span_start) {
// print it out:
echo "$span_start..$span_end\n";
}
$span_start = $day;
}
$span_end = $day;
}
// print the last one:
echo "$span_start..$span_end";
Output: (Demo)
2..6
23..25
30..30
For formatting/parsing/printing the dates, however, others' suggestion to use the built in time
and date
functions are absolutely correct.