如何使用PHP将一小时缩短到最近的2小时间隔?

时间:2015-10-19 20:20:13

标签: php

我有4个时间值:

$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');

我想将24小时时间间隔为2小时,所以我将有12小时,例如:0:00:01-1:59:59, 2:00:00-3:59:59, 4:00:00-5:59:59, etc.

我想要做的是将这些值放入这些区间。所以最终的结果应该是:

$result = array(
    '16:00:00-17:59:59' => array('16:04:12', '16:57:54', '17:59:59'),
    '18:00:00-19:59:59' => array('18:00:00')
);

我试图创建一个带有间隔的数组,然后循环检查它是否比另一个更大,但它没有按预期工作。

如何将这些时间分为2小时?

4 个答案:

答案 0 :(得分:1)

您必须创建两个小时组并在$result中初始化为空数组     

$result = array(
    '16:00:00-17:59:59' => array(),
    '18:00:00-19:59:59' => array()
);

for($i=0;$i<count($times);$i++)
{
        $date = strtotime($times[$i]);
        $hour= date('H', $date);

        if($hour%2==0)
            $hour=$hour;
        else 
            $hour=$hour-1;  

        foreach ($result as $key => $value) {
            if(substr($key,0,2)===$hour)
            {
                if(count($result[$key])==0)
                    $result[$key][0]=$times[$i];
                else
                    $result[$key][count($result[$key])+1]=$times[$i];               
            }
        }
}
print_r($result);
?>

<强> OUPUT

  

数组([16:00:00-17:59:59] =&gt;数组([0] =&gt; 16:04:12 [2] =&gt; 16:57:54   )[18:00:00-19:59:59] =&gt;数组([0] =&gt; 18:00:00))

答案 1 :(得分:1)

如果你想拥有一个包含所有time_intervals的数组,即使该时间段没有时间,你也可以使用以下代码。因此,如果您想跳过那些在其区域中没有时间的句点,您可以执行foreach并跳过那些具有空值的键。

$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
$interval = 2;
$time_intervals = array();
//Populate time intervals
for($i = 0; $i < 23; $i += $interval) {
    $from = str_pad($i,  2, "0", STR_PAD_LEFT).':00:00';
    $to = str_pad($i + 1,  2, "0", STR_PAD_LEFT).':59:59';
    $time_intervals[$from.'-'.$to] = array();
}
//Check through all time values
foreach($times as $time) {
    foreach($time_intervals as $key => $value) {
        $zone = explode('-', $key);
        $from = $zone[0];
        $to = $zone[1];
        if (strtotime($time) >= strtotime($from) && strtotime($time) <= strtotime($to)) {
            $time_intervals[$key][] = $time;
        }
    }
}
var_dump($time_intervals);

结果:

array (size=12)
  '00:00:00-01:59:59' => 
    array (size=0)
      empty
  '02:00:00-03:59:59' => 
    array (size=0)
      empty
  '04:00:00-05:59:59' => 
    array (size=0)
      empty
  '06:00:00-07:59:59' => 
    array (size=0)
      empty
  '08:00:00-09:59:59' => 
    array (size=0)
      empty
  '10:00:00-11:59:59' => 
    array (size=0)
      empty
  '12:00:00-13:59:59' => 
    array (size=0)
      empty
  '14:00:00-15:59:59' => 
    array (size=0)
      empty
  '16:00:00-17:59:59' => 
    array (size=3)
      0 => string '16:04:12' (length=8)
      1 => string '16:57:54' (length=8)
      2 => string '17:59:59' (length=8)
  '18:00:00-19:59:59' => 
    array (size=1)
      0 => string '18:00:00' (length=8)
  '20:00:00-21:59:59' => 
    array (size=0)
      empty
  '22:00:00-23:59:59' => 
    array (size=0)
      empty

答案 2 :(得分:1)

我认为在你的情况下你应该比较几小时 并且不要写一些复杂且难以理解的代码。
例如:

<?php

$times = array('16:04:12', '16:57:54', '17:59:59', '18:00:00');
foreach ($times as $t) {
    // extract hour from string
    $h = substr($t, 0, 2);
    if ($h%2 === 0) {
        $key = sprintf('%02d:00:00-%02d:59:59', $h, $h+2);
    } else {
        $key = sprintf('%02d:00:00-%02d:59:59', $h-1, $h+1);
    }
    // this key will resolve all values
    $r[$key][] = $t;
}
var_export($r);

这将打印:

array (
  '16:00:00-18:59:59' => 
  array (
    0 => '16:04:12',
    1 => '16:57:54',
    2 => '17:59:59',
  ),
  '18:00:00-20:59:59' => 
  array (
    0 => '18:00:00',
  ),
)

希望它会对你有所帮助,因为这段代码看起来很容易理解......

答案 3 :(得分:0)

要获得2小时的间隔,只需将模2添加到时间

例如

for($x=1;$x<=23;$x++)
{
 echo $x.': '.($x+$x%2).'<br />';
}