Jump to content

[SOLVED] Search in an array for the next day a firefighter is working


snowman2344

Recommended Posts

Hello Coders

I have the following array that is searched by date to get the letter corresponding to a firefighters shift schedule (Works great thanks to a coder on this forum). I now need to know the next time a shift is working. For example if ‘B’ shift is working today (if B corresponds to the 11th item in the array) the next time that ‘B’ is working would be the 19th spot in the array and the other shifts would work the following ‘A’=12 ‘C’=15 ‘D’=13. Form that I can get a date by adding to the current date.  So now I have got a loop sort of working that will find the next date but it is not working correctly.

 

 

 

$start_date = mktime(7, 0, 0, 7, 1, 2007);
$shift_cycle = array('C',// 0
				 'A','B','C','A','D','C','D','B','C','D',//1 to 10
		         'B','A','D','A','C','D','A','C','B','A',//11 to 20
				 'B','D','A','B','D','C','B');//21 to 27
$today = time();
//$today = mktime(6, 59, 0, 12, 12, 2007); // (Hr,Min,Sec,Mth,Day,Yr) to test

// Find the shift on today THIS WORKS FINE
$szz = $shift_cycle[((($today - $start_date)/(86400))-1)%28];//Finds Yesterday
$ss = $shift_cycle[(($today - $start_date)/(86400))%28];//Finds Today
$sxx = $shift_cycle[(1+(($today - $start_date)/(86400)))%28];//Finds Tommorow

// Start Looking for the next shift  THIS IS NOT WORKING RIGHT
$num = 1+(($today - $start_date)/(86400))%28;
$str = array('A','B','C','D');
foreach ($str as $s){
//$s = 'B';
unset ($ft_key, $i);
$ftf = false;
for ($i=$num;$i<28;$i++)
{
//if (in_array($s,$shift_cycle)) {
if ($s == $shift_cycle[$i]) {
$ftf = true;
$ft_key = $i;
break;
   }
   }

if ($ftf){
echo $s;echo' is working ';echo $ft_key;echo '<br>';

echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>";
}
};

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.