选择一段时间

时间:2014-07-30 10:29:07

标签: php time

我需要根据给定的开始时间和结束时间(UNIX TIME)期间数组中选择

请注意,可以选择多个期间

嘛!请考虑以下Php代码:

// User selected time
$startTime = time();// User selected start time
$endTime = $startTime+(((60*60)*24)*90); // User selected end time

// Periods
$periods = array(); // Periods array

// 1st period
$periods["p1id"] = 1; // 1st period id
$periods["p1st"] = 1410213600; // 1st period start time
$periods["p1et"] = 1418597940; // 1st period end time

// 2nd period
$periods["p2id"] = 2;
$periods["p2st"] = 1418598000;
$periods["p2et"] = 1420066740;

$periods["p3id"] = 3;
$periods["p3st"] = 1388530800;
$periods["p3et"] = 1389135540;

$periods["p4id"] = 4;
$periods["p4st"] = 1389135600;
$periods["p4et"] = 1394924340;

$periods["p5id"] = 5;
$periods["p5st"] = 1394924400;
$periods["p5et"] = 1398895140;

$periods["p6id"] = 6;
$periods["p6st"] = 1398895200;
$periods["p6et"] = 1404165540;

$periods["p7id"] = 7;
$periods["p7st"] = 1404165600;
$periods["p7et"] = 1410213540;

谢谢!

3 个答案:

答案 0 :(得分:0)

我不认为你的阵列结构很好。我建议这个:

$periods = [
   [
       'id' => 1,
       'start' => 1410213600,
       'end' => 1418597940,
   ],
   /* ... */
   [
       'id' => 7,
       'start' => 1404165600,
       'end' => 1410213540,
   ]
];

然后循环该数组并检查一些条件:

$periodsSelected = [];

foreach ($preiods as $period) {
    if ($startTime >= $period['start'] && $startTime <= $period['end']
       && $endTime >= $period['start'] && $endTime <= $period['end']
    ) {
       $periodsSelected[] = $period;
    }
}

答案 1 :(得分:0)

首先我建议你将句点数组转换成这样的结果:

// User selected time
$startTime = time();// User selected start time
$endTime = $startTime+(((60*60)*24)*90); // User selected end time

// Periods
$periods = array(); // Periods array

// 1st period
$periods[1]["start"] = 1410213600; // 1st period start time
$periods[1]["end"] = 1418597940; // 1st period end time

// 2nd period
$periods[2]["start"] = 1418598000;
$periods[2]["end"] = 1420066740;

在此之后如何选择时间段,这里

$selectPeriods = array();
    foreach ($periods as $id => $val) {
        if ($val["start"] > $startTime || $val["end"] < $endTime) {
    $selectPeriods[$id] = $periods[$id];
    }
}

答案 2 :(得分:0)

如上所述,结构不是最理想的。因此,我使用array_keys()迭代它:

// define array of ranges to select
$selectPeriodRange = array(
    0 => array(
        'start' => 1410213540,
        'end'   => 1418598000
    )
);

// have an empty array to store the results
$valid = array();

// it's easier to iterate by numeric index, since three entries are needed for each period
$period_keys = array_keys($periods);

// iterate over all desired ranges
foreach($selectPeriodRange as $prk => $prv)  {
    // iterate over periods, incrementing steps by 3
    for ($i=0; $i<sizeof($period_keys); $i+=3)  {
        // compare
        if ($periods[$period_keys[$i+1]] >= $prv['start'] && $periods[$period_keys[$i+2]] <  $prv['end'])  {
            // store the three period entries to the result array
            $valid[] = array(
                'pid' => $periods[$period_keys[$i]],
                'st'  => $periods[$period_keys[$i+1]],
                'et'  => $periods[$period_keys[$i+2]]
            );
        }
    }
}

echo '<pre>';
print_r($valid);
echo '</pre>';

注意:这假定pXet&gt; pXst。

希望这有帮助。