Radical_Activity
Radical_Activity

Reputation: 2738

How to round a hour to the nearest 2 hour interval with PHP?

I have 4 time values:

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

I would like to divide the 24 hour time to 2 hour intervals so I will have 12 hours, like: 0:00:01-1:59:59, 2:00:00-3:59:59, 4:00:00-5:59:59, etc.

What I want to do is place these values into those intervals. So the final result should be:

$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')
);

I have tried to create an array with the intervals and then loop through it checking if one is bigger than the other, but it did not work as intended.

How is it possible to put the hours into these 2 hour intervals?

Upvotes: 0

Views: 121

Answers (4)

cn0047
cn0047

Reputation: 17061

I think in your case you should just compare hours
and don't write some sophisticated and unreadable code.
For example:

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

This will prints:

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

Hope it will help you, because this code looks very understandable...

Upvotes: 1

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

If you want to have an array with all the time_intervals even if no time exists for that period then you can use the following code. So if you want to skip those periods that have not times in their zone you can do a foreach and skip those keys that have empty values.

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

Result:

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

Upvotes: 1

Rohan Khude
Rohan Khude

Reputation: 4883

You have to create two hour groups and initialize as empty array in $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

Array ( [16:00:00-17:59:59] => Array ( [0] => 16:04:12 [2] => 16:57:54 ) [18:00:00-19:59:59] => Array ( [0] => 18:00:00 ) )

Upvotes: 1

humer
humer

Reputation: 41

To get a 2 hour interval, just add modulo 2 to the time

example

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

Upvotes: 0

Related Questions