我有一段时间(旅行),例如2015-04-01
- 2015-04-23
。
现在,还有其他几个时期(条件),例如:
2015-03-27
- 2015-04-02
2015-04-04
- 2015-04-18
2015-04-02
- 2015-04-06
2015-04-14
- 2015-04-16
2015-04-23
- 2015-04-30
这里是草图(黑色是旅行,红色是条件期间)
目标是在没有间隙(如果可能),重叠和重复的情况下覆盖整个旅程。
所以我的最终条件是这样的:
2015-04-01
- 2015-04-02
2015-04-02
- 2015-04-03
2015-04-03
- 2015-04-18
2015-04-23
- 2015-04-23
新草图:
当我得到条件时,条件没有特定的顺序。
我目前的实施非常庞大且有缺陷。有没有一种行之有效的方法呢?
请注意,期间日期是示例,草图并不准确。
答案 0 :(得分:0)
您好,这是" quck "演示...... 我希望能够清楚地理解。
您的约会对象:
$travel = array('from'=>'20150401', 'to'=>'20150423');
$conditions = array(
'condition_1' => array('from'=>'20150327', 'to'=>'20150402'),
'condition_2' => array('from'=>'20150404', 'to'=>'20150418'),
'condition_3' => array('from'=>'20150402', 'to'=>'20150406'),
'condition_4' => array('from'=>'20150414', 'to'=>'20150416'),
'condition_5' => array('from'=>'20150423', 'to'=>'20150430')
);
准备日期的功能:
// CONVERTS DATES INTO TIME
function get_times($from, $to){
preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', preg_replace("/\D/m", '', $from), $d); $from = @mktime(0, 0, 0, $d[2], $d[3], $d[1]);
preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', preg_replace("/\D/m", '', $to), $d); $to = @mktime(0, 0, 0, $d[2], $d[3], $d[1]);
$delta = $to-$from;
return array('from'=>$from, 'to'=>$to, 'delta'=>$delta, 'days'=>$delta/86400);
}
// PARSE MORE DATA INTO $travel & $conditions
function parse_dates($travel, $conditions){
$travel['times'] = get_times($travel['from'], $travel['to']);
$applicable_conditions = array();
foreach($conditions as $i=>$cond){
if($cond['from']<=$travel['to'] && $cond['to']>=$travel['from']){
$cond['times'] = get_times($cond['from']<$travel['from']?$travel['from']:$cond['from'], $cond['to']>$travel['to']?$travel['to']:$cond['to']);
$applicable_conditions[$i] = $cond;
}
}
return array('travel'=>$travel, 'conditions'=>$applicable_conditions);
}
举个例子,你可能想把它变成更适合你的东西:
// (mockup) DISPLAY GRAPHICS
function show_graph($dates){
$w = 600; $w2 = 100; $h = 30; $bg = '#4682B4'; $col = '#FFFFFF';
$day = '<div style="display:inline-block; width:'.(round($w/$dates['travel']['times']['days'])-2).'px; border:1px solid '.$col.'; height:'.($h-2).'px;';
$day_blue = $day.' background-color:'.$bg.';">';
$day_red = $day.' background-color:red;">';
$day_empty = $day.' background-color:white;">';
$day_black = $day.' background-color:black;">';
$cd = '</div>';
$days = ''; $cond_days = $day_nocond = array();
for($i=1; $i<=$dates['travel']['times']['days']; $i++){
$daytime = $dates['travel']['times']['from']+(86400*$i);
$days .= $day_blue.$i.$cd;
$day_havecond[$i] = false;
foreach($dates['conditions'] as $name=>$cond){
$is_cond = $daytime>=$cond['times']['from'] && $daytime<=$cond['times']['to'];
$day_box = $is_cond ? ( $day_havecond[$i] ? $day_black.$i.$cd : $day_red.$i.$cd ) : $day_empty.' '.$cd;
if($is_cond && !$day_havecond[$i]){ $day_havecond[$i] = true;}
@$cond_days[$name] .= $day_box;
}
}
$o = '
<div style="display:inline-block; width:'.$w2.'px;">Travel</div>
<div style="display:inline-block; color:'.$col.';">'.$days.'</div><br/>';
foreach($cond_days as $name=>$days){
$o .= '
<div style="display:inline-block; width:'.$w2.'px;">'.$name.'</div>
<div style="display:inline-block; color:'.$col.';">'.$days.'</div><br/>';
}
return '<div style="width:'.($w+$w2).'px;">'.$o.'</div>' ;
}
这就是你制作图形的方法:
# USAGE
$dates = parse_dates($travel, $conditions);
echo show_graph($dates);
# DEBUG
echo '<hr>'; print_r($dates);