esport Posted October 7, 2007 Share Posted October 7, 2007 Hey Guys, I have and array of dates for example: $calDAtes = array(20071001,20071002,20071003,20071004,20071006,20071008,20071009,20071010,20071011); Now I want to create another array that stores the first and last date that are consecutive. So the above example will be: $dateArray[0]['firstDate'] = 20071001 , $dateArray[0]['lasttDate'] = 20071004 $dateArray[1]['firstDate'] = 20071006 ,$dateArray[1]['lasttDate'] = 20071006 $dateArray[2]['firstDate'] = 20071008 ,$dateArray[2]['lasttDate'] = 200710011 Any help would be much appreciated. Daniel Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2007 Share Posted October 7, 2007 well, in case the dates aren't sorted, sort them: $calDAtes = sort($calDAtes); Then pair each... pair, something like this: $a_count = 0; for ($i=0;$i<count($calDAtes);$i+=2) { $dateArray[$a_count]['firstDate'] = $calDAtes[$i]; $dateArray[$a_count]['firstDate'] = $calDAtes[$i + 1]; $a_count++; } Quote Link to comment Share on other sites More sharing options...
esport Posted October 7, 2007 Author Share Posted October 7, 2007 Thanks for you response, however your only pairing every second entry. I only want to pair the first date and last date that are in a sequence. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 7, 2007 Share Posted October 7, 2007 try <?php $calDates = array(20071001,20071002,20071003,20071004,20071006,20071008,20071009,20071010,20071011); $result = array(); $j=0; for ($i=0, $k=count($calDates); $i<$k-1; $i++) { if (!isset($result[$j][0]))$result[$j] = array($calDates[$i], $calDates[$i]); if ($calDates[$i] == date('Ymd', strtotime("-1 day {$calDates[$i+1]}"))) { $result[$j][1] = $calDates[$i+1]; } else { $j++; } } --> Array ( [0] => Array ( [0] => 20071001 [1] => 20071004 ) [1] => Array ( [0] => 20071006 [1] => 20071006 ) [2] => Array ( [0] => 20071008 [1] => 20071011 ) ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.