基于PHP中的时间间隔拆分数组

时间:2012-05-03 20:16:54

标签: php arrays

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

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
           )
)

这样做的好方法是什么?

3 个答案:

答案 0 :(得分:1)

让您走上正轨的一切事情:

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

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

  3. 如果差异<= =间隔,则表示您已完成

  4. 如果差异&gt; interval,创建新数组并为init添加时间间隔。转到第2步

  5. 向我们展示代码!

答案 1 :(得分: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)

答案 2 :(得分: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
        )

)