分裂日期

时间:2012-10-26 11:54:00

标签: php date datetime

$one = array('start' => '2012-10-12', 'stop' => '2012-11-04', 'over' => false);
$two = array('start' => '2012-10-29', 'stop' => '2012-11-14', 'over' => true);
$three = array('start' => '2012-11-12', 'stop' => '2012-12-07', 'over' => false);

$array = array($one, $two, $three);
这告诉我:

Array
(
    [0] => Array
        (
            [start] => 2012-10-12
            [stop] => 2012-11-04
            [over] => 
        )

    [1] => Array
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )

    [2] => Array
        (
            [start] => 2012-11-12
            [stop] => 2012-12-07
            [over] => 
        )

)

我想制作分割这些日期的功能。 如果在数组中 over = true ,则此日期应分割其他日期。

对于这个例子,这应该返回:

Array
(
    [0] => Array                  //this is changed
        (
            [start] => 2012-10-12
            [stop] => 2012-10-28
            [over] => 
        )

    [1] => Array                   //this is ok
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )

    [2] => Array                  //this is changed
        (
            [start] => 2012-11-15
            [stop] => 2012-12-07
            [over] => 
        )

)

第二个例子:

$one = array('start' => '2012-10-12', 'stop' => '2012-12-04', 'over' => false);
$two = array('start' => '2012-10-29', 'stop' => '2012-11-14', 'over' => true);


$second = array($one, $two);

这回报我:

Array
(
    [0] => Array
        (
            [start] => 2012-10-12
            [stop] => 2012-12-04
            [over] => 
        )

    [1] => Array
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )

)

我希望收到:

Array
(
    [0] => Array                  //this is changed - old [0] date
        (
            [start] => 2012-10-12
            [stop] => 2012-10-28
            [over] => 
        )

    [1] => Array                  //this is ok
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )
    [2] => Array                  //this is new - from old [0] date - next part
        (
            [start] => 2012-11-15
            [stop] => 2012-12-04
            [over] => 
        )
)

我该如何使用PHP?

1 个答案:

答案 0 :(得分:0)

试试这个。

foreach($arr as $key=>$val)
{
   if(isset($arr[$key+1]) && $arr[$key+1]['over'] == true)
       $arr[$key]['stop'] = date('Y-m-d', strtotime($arr[$key+1]['start'] .' -1 day'));
  if(isset($arr[$key-1]) && $arr[$key-1]['over'] == true)
       $arr[$key]['start'] = date('Y-m-d', strtotime($arr[$key-1]['stop'] .' +1 day'));
}

你可以在这里找到code

<强>更新

 $arr1 = $arr;
 foreach($arr as $key=>$val)
{
   if(isset($arr[$key+1]) && $arr[$key+1]['over'] == true)
   {
       $arr[$key]['stop'] = date('Y-m-d', strtotime($arr[$key+1]['start'] .' -1 day'));
       $arr[$key]['over'] = true;
   }
   if(isset($arr[$key-1]) && $arr[$key-1]['over'] == true)
   {
       $arr[$key]['start'] = date('Y-m-d', strtotime($arr[$key-1]['stop'] .' +1 day'));
       $arr[$key]['over'] = true;
   }
 }

foreach($arr1 as $key=>$val)
{
   $arr[$key]['over'] = $arr1[$key]['over'];
}

已更新 code此处。