Jump to content

Simplify?


jmaccs64

Recommended Posts

Hello all,

 

My simple programing mind has led me to this implementation of a function. I work at a school, there are three different bell schedules one for Mon&Fri one for Tues&Thurs and one for Wed. I simply want to print on our homepage the time the current Period Ends. Here is what I have, it seems to work, but i am sure there is a way to do this with some while statements maybe even a foreach! Is there anyway someone can show me the error in my ways!

 

Thanks in Advance!

Joe

 

<?php

function getperiod(){
date_default_timezone_set('America/Chicago');
$mon_fri = array(725,810,815,900,906,921,927,1012,1018,1103,1109,1154,1200,1245,1250,1335,1340,1425);
$tues_thur = array(725,813,818,907,912,1000,1005,1053,1058,1146,1151,1239,1244,1332,1337,1425);
$wednesday = array(745,830,835,920,926,1011,1017,1102,1108,1153,1159,1244,1250,1335,1340,1425);
$day = date('N');
$time = date('Hi');
// If it is Monday or Friday
if (($day == 1) OR ($day == 5)){
	if (($mon_fri[0] < $time) AND ($time < $mon_fri[1])){ echo "Period 1 ends at 8:10am";}
elseif (($mon_fri[2] < $time) AND ($time < $mon_fri[3])){ echo "Period 2 ends at 9:00am";}
elseif (($mon_fri[4] < $time) AND ($time < $mon_fri[5])){ echo "Period 3 ends at 9:21am";}
elseif (($mon_fri[6] < $time) AND ($time < $mon_fri[7])){ echo "Period 4 ends at 10:12am";}
elseif (($mon_fri[8] < $time) AND ($time < $mon_fri[9])){ echo "Period 5 ends at 11:03am";}
elseif (($mon_fri[10] < $time) AND ($time < $mon_fri[11])){ echo "Period 6 ends at 11:54pm";}
elseif (($mon_fri[12] < $time) AND ($time < $mon_fri[13])){ echo "Period 7 ends at 12:45pm";}
elseif (($mon_fri[14] < $time) AND ($time < $mon_fri[15])){ echo "Period 8 ends at 1:35pm";}
elseif (($mon_fri[16] < $time) AND ($time < $mon_fri[17])){ echo "Period 9 ends at 2:25pm";}
else { echo "School Is Out!";}
}

// If it is Tuesday or Thurs
elseif (($day == 2) OR ($day == 4)){
	if (($tues_thur[0] < $time) AND ($time < $tues_thur[1])){ echo "Period 1 ends at 8:13am";}
elseif (($tues_thur[2] < $time) AND ($time < $tues_thur[3])){ echo "Period 2 ends at 9:07am";}
elseif (($tues_thur[4] < $time) AND ($time < $tues_thur[5])){ echo "Period 4 ends at 10:00am";}
elseif (($tues_thur[6] < $time) AND ($time < $tues_thur[7])){ echo "Period 5 ends at 10:53am";}
elseif (($tues_thur[8] < $time) AND ($time < $tues_thur[9])){ echo "Period 6 ends at 11:46am";}
elseif (($tues_thur[10] < $time) AND ($time < $tues_thur[11])){ echo "Period 7 ends at 12:39pm";}
elseif (($tues_thur[12] < $time) AND ($time < $tues_thur[13])){ echo "Period 8 ends at 1:32pm";}
elseif (($tues_thur[14] < $time) AND ($time < $tues_thur[15])){ echo "Period 9 ends at 2:25pm";}
else { echo "School Is Out!";}
}

// If it is Wednesday
elseif ($day == 3){
	if (($wednesday[0] < $time) AND ($time < $wednesday[1])){ echo "Period 1 ends at 8:30am";}
elseif (($wednesday[2] < $time) AND ($time < $wednesday[3])){ echo "Period 2 ends at 9:20am";}
elseif (($wednesday[4] < $time) AND ($time < $wednesday[5])){ echo "Period 4 ends at 10:11am";}
elseif (($wednesday[6] < $time) AND ($time < $wednesday[7])){ echo "Period 5 ends at 11:02am";}
elseif (($wednesday[8] < $time) AND ($time < $wednesday[9])){ echo "Period 6 ends at 11:53am";}
elseif (($wednesday[10] < $time) AND ($time < $wednesday[11])){ echo "Period 7 ends at 12:44pm";}
elseif (($wednesday[12] < $time) AND ($time < $wednesday[13])){ echo "Period 8 ends at 1:35pm";}
elseif (($wednesday[14] < $time) AND ($time < $wednesday[15])){ echo "Period 9 ends at 2:25pm";}
else { echo "School Is Out!";}

}

elseif (($day == 6) OR ($day == 7)){ echo "It's the weekend!";}
}



?>

Link to comment
Share on other sites

I did the first two for you... just wednesday left to do...

 

<?php

function getperiod()
{
date_default_timezone_set('America/Chicago');
$mon_fri = array(725,810,815,900,906,921,927,1012,1018,1103,1109,1154,1200,1245,1250,1335,1340,1425);
$tues_thur = array(725,813,818,907,912,1000,1005,1053,1058,1146,1151,1239,1244,1332,1337,1425);
$wednesday = array(745,830,835,920,926,1011,1017,1102,1108,1153,1159,1244,1250,1335,1340,1425);
$day = date('N');
$time = date('Hi');

// If it is Monday or Friday
if (($day == 1) OR ($day == 5))
{
	for($i=0; $i<count($mon_fri); $i++)
	{
		if (($mon_fri[$i] < $time) AND ($time < $mon_fri[$i+1]))
		{ 
			$result = "Period $i ends at $mon_fri[$i]";
		}
	}
	//if we did not set a result school is out
	if(empty($result))
	{
		echo "School is Out!";
	}
	//else display it
	else
	{
		echo $result;
	}

}

// If it is Tuesday or Thurs
elseif (($day == 2) OR ($day == 4))
{
	for($i=0; $i<count($tues_thur); $i++)
	{
		if (($tues_thur[$i] < $time) AND ($time < $tues_thur[$i+1]))
		{
			$result = "Period $i ends at $tues_thur[$i]";
		}
	}
	if(empty($result))
	{
		echo "School is Out!";
	}
	else
	{
		echo $result;
	}
}

// If it is Wednesday
elseif ($day == 3)
{
	  if (($wednesday[0] < $time) AND ($time < $wednesday[1])){ echo "Period 1 ends at 8:30am";}
   elseif (($wednesday[2] < $time) AND ($time < $wednesday[3])){ echo "Period 2 ends at 9:20am";}
   elseif (($wednesday[4] < $time) AND ($time < $wednesday[5])){ echo "Period 4 ends at 10:11am";}
   elseif (($wednesday[6] < $time) AND ($time < $wednesday[7])){ echo "Period 5 ends at 11:02am";}
   elseif (($wednesday[8] < $time) AND ($time < $wednesday[9])){ echo "Period 6 ends at 11:53am";}
   elseif (($wednesday[10] < $time) AND ($time < $wednesday[11])){ echo "Period 7 ends at 12:44pm";}
   elseif (($wednesday[12] < $time) AND ($time < $wednesday[13])){ echo "Period 8 ends at 1:35pm";}
   elseif (($wednesday[14] < $time) AND ($time < $wednesday[15])){ echo "Period 9 ends at 2:25pm";}
   else { echo "School Is Out!";}

}

elseif (($day == 6) OR ($day == 7)){ echo "It's the weekend!";}
}


?>

 

maybe you could get the students to refine it futher as a bit of home work ;)

Link to comment
Share on other sites

i spotted a few mistakes so i fixed them and added wednesday in for you too..

 

<?php

function getperiod()
{
date_default_timezone_set('America/Chicago');
$mon_fri = array(725,810,815,900,906,921,927,1012,1018,1103,1109,1154,1200,1245,1250,1335,1340,1425);
$tues_thur = array(725,813,818,907,912,1000,1005,1053,1058,1146,1151,1239,1244,1332,1337,1425);
$wednesday = array(745,830,835,920,926,1011,1017,1102,1108,1153,1159,1244,1250,1335,1340,1425);
$day = date('N');
$time = date('Hi');

// If it is Monday or Friday
if (($day == 1) OR ($day == 5))
{
	for($i=0; $i<count($mon_fri) -1; $i++)
	{
		if (($mon_fri[$i] < $time) AND ($time < $mon_fri[$i+1]))
		{
			$period = $i+1;
			$result = "Period $period ends at " . $mon_fri[$period];
		}
	}
	//if we did not set a result school is out
	if(empty($result))
	{
		echo "School is Out!";
	}
	//else display it
	else
	{
		echo $result;
	}

}

// If it is Tuesday or Thurs
elseif (($day == 2) OR ($day == 4))
{
	for($i=0; $i<count($tues_thur) -1; $i++)
	{
		if (($tues_thur[$i] < $time) AND ($time < $tues_thur[$i+1]))
		{
			$period = $i+1;
			$result = "Period $period ends at " . $tues_thur[$period];
		}
	}
	if(empty($result))
	{
		echo "School is Out!";
	}
	else
	{
		echo $result;
	}
}

// If it is Wednesday
elseif ($day == 3)
{
	for($i=0; $i<count($wednesday) -1; $i++)
	{
		if (($wednesday[$i] < $time) AND ($time < $wednesday[$i+1]))
		{
			$period = $i+1;
			$result = "Period $period ends at " . $tues_thur[$period];
		}
	}
	if(empty($result))
	{
		echo "School is Out!";
	}
	else
	{
		echo $result;
	}

}

elseif (($day == 6) OR ($day == 7)){ echo "It's the weekend!";}
}
?>

Link to comment
Share on other sites

Had nothing to do so I added some formatting.

 

<?php
function getperiod()
{
date_default_timezone_set('America/Chicago');
$mon_fri = array(725,810,815,900,906,921,927,1012,1018,1103,1109,1154,1200,1245,1250,1335,1340,1425);
$tues_thur = array(725,813,818,907,912,1000,1005,1053,1058,1146,1151,1239,1244,1332,1337,1425);
$wednesday = array(745,830,835,920,926,1011,1017,1102,1108,1153,1159,1244,1250,1335,1340,1425);
$day  = date('N');
$time = date('Hi');

// If it is Monday or Friday
if (($day == 1) OR ($day == 5))
{
	for($i=0; $i < count($mon_fri) - 1; $i++)
	{
		if (($mon_fri[$i] <= $time) AND ($time < $mon_fri[$i+1]))
		{
			$period = $i+1;

			if ( strlen( $mon_fri[$period] ) == 3 )
			{
			   $end = substr($mon_fri[$period], 0, 1) . ":" . substr($mon_fri[$period], -2) . "am";
			}
			elseif ( strlen( $mon_fri[$period] ) == 4 && substr( $mon_fri[$period], 0, 2 ) < 12 )
			{
			   $end = substr( $mon_fri[$period], 0, 2 ) . ":" . substr( $mon_fri[$period], -2 ) . "am";
			}
			else
			{
			   $end = ( substr( $mon_fri[$period], 0, 2 ) - 12 ) . ":" . substr($mon_fri[$period], -2) . "pm";
			}

			$result = "Period $period ends at " . $end;
		}
	}
	//if we did not set a result school is out
	if(empty($result))
	{
		echo "School is Out!";
	}
	//else display it
	else
	{
		echo $result;
	}

}

// If it is Tuesday or Thurs
elseif (($day == 2) OR ($day == 4))
{
	for($i=0; $i < count($tues_thur) - 1; $i++)
	{
		if (($tues_thur[$i] <= $time) AND ($time < $tues_thur[$i+1]))
		{
			$period = $i+1;

			if ( strlen( $tues_thur[$period] ) == 3 )
			{
			   $end = substr($tues_thur[$period], 0, 1) . ":" . substr($tues_thur[$period], -2) . "am";
			}
			elseif ( strlen( $tues_thur[$period] ) == 4 && substr( $tues_thur[$period], 0, 2 ) < 12 )
			{
			   $end = substr( $tues_thur[$period], 0, 2 ) . ":" . substr( $tues_thur[$period], -2 ) . "am";
			}
			else
			{
			   $end = ( substr( $tues_thur[$period], 0, 2 ) - 12 ) . ":" . substr($tues_thur[$period], -2) . "pm";
			}

			$result = "Period $period ends at " . $end;
		}
	}
	if(empty($result))
	{
		echo "School is Out!";
	}
	else
	{
		echo $result;
	}
}

// If it is Wednesday
elseif ($day == 3)
{
	for($i=0; $i < count($wednesday) - 1; $i++)
	{
		if (($wednesday[$i] <= $time) AND ($time < $wednesday[$i+1]))
		{
			$period = $i+1;

			if ( strlen( $wednesday[$period] ) == 3 )
			{
			   $end = substr($wednesday[$period], 0, 1) . ":" . substr($wednesday[$period], -2) . "am";
			}
			elseif ( strlen( $wednesday[$period] ) == 4 && substr( $wednesday[$period], 0, 2 ) < 12 )
			{
			   $end = substr( $wednesday[$period], 0, 2 ) . ":" . substr( $wednesday[$period], -2 ) . "am";
			}
			else
			{
			   $end = ( substr( $wednesday[$period], 0, 2 ) - 12 ) . ":" . substr($wednesday[$period], -2) . "pm";
			}

			$result = "Period $period ends at " . $end;
		}
	}
	if(empty($result))
	{
		echo "School is Out!";
	}
	else
	{
		echo $result;
	}

}
elseif (($day == 6) OR ($day == 7))
{
   echo "It's the weekend!";
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.