Help with Consecutive Time Values in Array

I need to take an array of times and filter through it and only display times that are consecutive 4 or more subsequently.

Here is what I have, but it is not solid. Any help would be great.

[php]
$avail_times = array(“9”,“11”,“12”,“13”,“14”,“16”,“17”,“18”,“19”,“20”,“21”,“22”);

for($i=1, $max = count($avail_times) + 4; $i < $max; $i++)
{
if ($avail_times[$i] == $avail_times[$i + 1] - 1)
{
echo $avail_times[$i];
echo “

”;
}
}[/php]

The result should be:

11
12
13
14
16
17
18
19
20
21
22

Hi there,

The following works from what I have tried, but it is fairly long. As this task has interested me I will look into a quicker, neater and easier way of doing it (which could well be more obvious than I realise, but it’s late at the moment!). Anyway, until I have a better way (or anyone else does for that matter), I will post what I have at the moment:

[php] $avail_times = array(“9”,“11”,“12”,“13”,“14”,“16”,“17”,“18”,“19”,“20”,“21”,“22”);
$groups = array(0 => array($avail_times[0])); //Put first time into first group array
$i = 0;
foreach($avail_times as $key => $time)
{
if($key != 0) //Ignore the first time, as it is already used
{
$last_time = !isset($groups[$i]) ? $time-1 : $groups[$i][count($groups[$i])-1]; //Get last set time
if($last_time + 1 == $time)
{
$groups[$i][] = $time; //Add this number to the end of a group
}
else
{
$i++; //Increment group count
$groups[$i][] = $time; //Create a new group with this time as the first one
}
}
}

	foreach($groups as $key => $times)
	{
		if(count($times) < 4)
		{
			unset($groups[$key]); //Unset any groups of less than 4
		}
	}
	
	sort($groups); //Reset top-level keys (group ids)

	echo "<pre>";
	print_r($groups); //Have a gander at what's left!
	echo "</pre>";

	/*
		This example looks like:

		Array
		(
			[0] => Array
				(
					[0] => 11
					[1] => 12
					[2] => 13
					[3] => 14
				)

			[1] => Array
				(
					[0] => 16
					[1] => 17
					[2] => 18
					[3] => 19
					[4] => 20
					[5] => 21
					[6] => 22
				)

		)
	 */[/php]

Thanks for your help. It is a great start for what I am trying to accomplish. I will post my finished product since my needs have now grown.

How would I take the arrays and put them into potential time blocks of four as check boxes?

For instance:

Checkbox 1 = 9:00 am – 12:00 pm
Checkbox 2 = 10:00 am – 1:00 pm
Checkbox 3 = 11:00 am – 2:00 pm

and so on…

I am doing this for a scheduling system that has some unique parameters.

Thanks in advance for any help.

Are the numbers going to be in 24 hour format? If they are in 12 hour that will make it harder to work out am or pm

Yes. They are in 24 hour format. Thanks so much for your help.

This is what I came up with and it works perfectly. Let me know if you have something different that works better. Thanks for all of your help.

[php]
$hours = ‘4’;
$options = ‘’;

if(count($groups) > 0)
{
foreach ($groups as $group)
{
for ($i = $hours; $i < count($group); ++$i)
{
$options .= “”.date(“g:i a”, strtotime($group[$i-$hours])). “-” . date(“g:i a”, strtotime($group[$i]))."
";
}
}

echo $options;

}
else
{
echo “There are no possible time blocks available. Please adjust your group size.”;
}
[/php]

To be completely honest your method is almost identical to what I was going to suggest, I feel no need to provide an alternative solution if all is working fine for you. Nicely done, and happy coding!

Sponsor our Newsletter | Privacy Policy | Terms of Service