4

我正在寻找一种基于数组中包含的时间间隔来拆分数组的方法。例如,

Array (
   [0] => Array (
               ['type'] => Text Format
               ['interval'] => 30
               ['init'] => 2012-04-30 09:00:00
               ['final'] => 2012-04-30 10:00:00
           )
   [1] => Array (
               ['type'] => Word Format
               ['interval'] => 12
               ['init'] => 2012-04-28 13:00:00
               ['final'] => 2012-04-28 13:30:00
           )
)

将其拆分为:

Array (
   [0] => Array (
               ['type'] => Text Format
               ['interval'] => 30
               ['init'] => 2012-04-30 09:00:00
               ['final'] => 2012-04-30 09:30:00
           )
   [1] => Array (
               ['type'] => Text Format
               ['interval'] => 30
               ['init'] => 2012-04-30 09:30:00
               ['final'] => 2012-04-30 10:00:00
           )
   [2] => Array (
               ['type'] => Word Format
               ['interval'] => 12
               ['init'] => 2012-04-28 13:00:00
               ['final'] => 2012-04-28 13:12:00
           )
   [3] => Array (
               ['type'] => Word Format
               ['interval'] => 12
               ['init'] => 2012-04-28 13:12:00
               ['final'] => 2012-04-28 13:24:00
           )
)

这样做的好方法是什么?

4

3 回答 3

1

鉴于您提供的内容,应该这样做:

foreach($old as $o) {
    $interval= $o['interval'];
    $new[] = $o;
    $x = count($new)-1;
    $new[$x]['final'] = date("Y-m-d H:i:s",strtotime("+$interval minutes", strtotime($old[$x]['init'])));

    $new[] = $o;
    $y = count($new)-1;
    $new[$y]['init'] = $new[$x]['final'];
    $new[$y]['final']   = date("Y-m-d H:i:s",strtotime("+$interval minutes", strtotime($new[$y]['init'])));
}

echo "Old:<pre>".print_r($old,true)."</pre>";
echo "New<pre>".print_r($new,true)."</pre>";

我测试的输出:

Old:
Array
(
    [0] => Array
        (
            [type] => Text Format
            [interval] => 30
            [init] => 2012-04-30 09:00:00
            [final] => 2012-04-30 10:00:00
        )

    [1] => Array
        (
            [type] => Word Format
            [interval] => 12
            [init] => 2012-04-28 13:00:00
            [final] => 2012-04-28 13:30:00
        )

)

New
Array
(
    [0] => Array
        (
            [type] => Text Format
            [interval] => 30
            [init] => 2012-04-30 09:00:00
            [final] => 2012-04-30 09:30:00
        )

    [1] => Array
        (
            [type] => Text Format
            [interval] => 30
            [init] => 2012-04-30 09:30:00
            [final] => 2012-04-30 10:00:00
        )

    [2] => Array
        (
            [type] => Word Format
            [interval] => 12
            [init] => 2012-04-28 13:00:00
            [final] => 1969-12-31 16:12:00
        )

    [3] => Array
        (
            [type] => Word Format
            [interval] => 12
            [init] => 1969-12-31 16:12:00
            [final] => 1969-12-31 16:24:00
        )

)
于 2012-05-03T21:14:07.253 回答
1

这可能对你有用......我只比较最多 20 个间隔

$dates = Array (
        0 => Array (
                'type' => "Text Format",
                'interval' => "30",
                'init' => "2012-04-30 09:00:00",
                'final' => "2012-04-30 10:00:00" 
        ),
        1 => Array (
                'type' => "Word Format",
                'interval' => 12,
                'init' => "2012-04-28 13:00:00",
                'final' => "2012-04-28 13:30:00" 
        ) 
);

echo "<pre>";

$output = array ();

foreach ( $dates as $key => $value ) {
    makeDates ( $value ['init'], $value ['final'], $value ['interval'], $value ['type'] ,$output);
}

var_dump ( $output );
function makeDates($start, $end, $min, $type, &$output) {
    $start = new DateTime ( $start );
    $end = new DateTime ( $end );

    $interval = new DateInterval ( 'PT' . $min . 'M' );

    for($i = 0; $i < 20; $i ++) {
        $list = array ();
        $list ['type'] = $type;
        $list ['interval'] = $min;
        $list ['init'] = $start->format ( 'Y-m-d H:i:s' );

        $start->add ( $interval );

        $list ['final'] = $start->format ( 'Y-m-d H:i:s' );

        if ($start == $end) {
            $output [] = $list;
        }

        if ($start >= $end) {
            break;
        }

        $output [] = $list;

    }

    return $output;
}

输出

array
  0 => 
    array
      'type' => string 'Text Format' (length=11)
      'interval' => string '30' (length=2)
      'init' => string '2012-04-30 09:00:00' (length=19)
      'final' => string '2012-04-30 09:30:00' (length=19)
  1 => 
    array
      'type' => string 'Text Format' (length=11)
      'interval' => string '30' (length=2)
      'init' => string '2012-04-30 09:30:00' (length=19)
      'final' => string '2012-04-30 10:00:00' (length=19)
  2 => 
    array
      'type' => string 'Word Format' (length=11)
      'interval' => int 12
      'init' => string '2012-04-28 13:00:00' (length=19)
      'final' => string '2012-04-28 13:12:00' (length=19)
  3 => 
    array
      'type' => string 'Word Format' (length=11)
      'interval' => int 12
      'init' => string '2012-04-28 13:12:00' (length=19)
      'final' => string '2012-04-28 13:24:00' (length=19)
于 2012-05-03T20:50:44.433 回答
1

有几件事可以让您走上正轨:

  1. 您不能直接在 PHP 中比较日期/时间字符串,将它们转换为时间戳

  2. 比较 init 和 final 并检查差异是否 > 间隔

  3. 如果差异<=间隔,你就完成了

  4. 如果差异 > 间隔,则创建新数组并将间隔添加到 init。转到第 2 步

向我们展示代码!

于 2012-05-03T20:47:55.540 回答