Berre Posted February 23, 2012 Share Posted February 23, 2012 I need some help with creating this algorithm. The goal is to find the highest occurrence of overlapping date ranges. Let's say I have some date ranges: 2012-02-21 - 2012-02-24 2012-02-21 - 2012-02-26 2012-02-23 - 2012-02-29 2012-02-24 - 2012-02-27 2012-02-26 - 2012-02-28 Of these the date 2012-02-24 and 2012-02-26 are overlapping 4 times, which is the highest number of overlapping dates. This number (4) is what I want to get. I don't care when the peak is, or how often it peaks, just the highest number of overlapping dates. It's not important with a lot of exact PHP (you don't have to write the entire thing), but I'm completely lost for a simple and fast way of solving this. Link to comment https://forums.phpfreaks.com/topic/257603-overlapping-date-range-peak/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 23, 2012 Share Posted February 23, 2012 In order to find the maximum sum, you would need to iterate over each range of dates and maintain a sum for each date, then simply find the maximum. <?php $data[] = array('2012-02-21','2012-02-24'); $data[] = array('2012-02-21','2012-02-26'); $data[] = array('2012-02-23','2012-02-29'); $data[] = array('2012-02-24','2012-02-27'); $data[] = array('2012-02-26','2012-02-28'); $arr = array(); foreach($data as $range){ while($range[0] <= $range[1]){ $arr[$range[0]] = isset($arr[$range[0]]) ? $arr[$range[0]] + 1 :1; $range[0] = date('Y-m-d',strtotime($range[0] . '+ 1 day')); } } arsort($arr); $peak = current($arr); echo "Peak: $peak"; Link to comment https://forums.phpfreaks.com/topic/257603-overlapping-date-range-peak/#findComment-1320330 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.