HELP : how to validate time interval. 8to10 conflict 9 to 11

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adisonz
Forum Newbie
Posts: 11
Joined: Wed Feb 08, 2006 2:09 am

HELP : how to validate time interval. 8to10 conflict 9 to 11

Post by adisonz »

08:00AM ~ 10:00AM = first row
11:00AM ~ 14:00PM = second row
12:00PM ~ 13:00PM = thrid row


frist row conflict with second row conflict with thrid row .

how to validate those conflict ? i used 5 hours to figure it but still blur * 100.

Please help me ...thanks you .
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

There is no such thing as "14:00PM" or "13:00PM". When you use the 24 hour system, AM and PM does not apply.

Anyway, since you're using the 24 hour clock system, just make sure the times are on the same day and if $nextstarttime is less hours from 00:00 than $lastendtime, then forward the script processing to an error handler.

(hope that makes sense)
adisonz
Forum Newbie
Posts: 11
Joined: Wed Feb 08, 2006 2:09 am

Post by adisonz »

but all those time interval is add in not by accending method .....user may select any time interval ....so how ?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I could not understand what the problem is...what do you mean by conflicting? ... what are these values and where are you using them..the context? :roll:
adisonz
Forum Newbie
Posts: 11
Joined: Wed Feb 08, 2006 2:09 am

Post by adisonz »

My question is

Tutorial class start on 8am , 2 hour class = 8am to 10am in Monday for IT student.


with the same IT student is not possible to attend another class on 9am to 10am in Monday .

i want to detect those thing .
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

can you show us how you store these details in the db???
Last edited by raghavan20 on Fri Feb 10, 2006 1:56 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Here are three basic cases

Code: Select all

No intersection:
                                                 B
                        A               |---------------|
            |------------------|  
        
---------------------------------------------------------------> time

Intersection:
                                                 B
                        A   |----------------------------|
            |------------------|  
        
---------------------------------------------------------------> time

Containment:
                                                 B
         |----------------------------------------------|
            |------------------|  
                        A
---------------------------------------------------------------> time
As you can see, the only case where intervals A and B have no intersecting region is the first one. Thus, intervals A and B do not intersect each other when none of the end points of A is between end points of B and vice versa
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You could maybe brute force a solution by filling a array with all the times that art blocked out and the looping through all the hours to check for conflicts. It seems a little crazy like there should be a better way, but:

Code: Select all

$schedule[1][8] = 1;
$schedule[1][9] = 1;
$schedule[1][10] = 1;

$schedule[2][11] = 1;
$schedule[2][12] = 1;
$schedule[2][13] = 1;
$schedule[2][14] = 1;

$schedule[3][12] = 1;
$schedule[3][13] = 1;

$num_schedules = 3;
for ($hour=0; $hour<24; ++$hour) {
    for ($sched=0; $sched<$num_schedules; ++$sched) {
        for ($sched2=0; $sched2<$num_schedules; ++$sched2) {
            if (($sched != $sched2) && $schedule[$sched][$hour] && $schedule[$sched2][$hour) {
                 $clash[] = "Schedule $sched clashes with schedule $sched2 at $hour:00\n";
            }
       }
}
(#10850)
adisonz
Forum Newbie
Posts: 11
Joined: Wed Feb 08, 2006 2:09 am

Post by adisonz »

This is the time table : Monday to Friday , i use for loop to loop all the tables and content .
then a checkbox checked for those row which willing to add in to the timetable.


~checkbox i use monday[] for monday timetable, tuesday[] for tuesday .
~array[] to store Monday to Saturday string.
~time[] to store time value
~$x for looping value from Monday to Saturday.
~$f value of checkbox.
~name of the input field $array[$x]."Start".$f

~Start and End id for monday frist row (MondayStart0) (MondayEnd0)
~let say i have 10 row ......how to check with your method ?.

thank you



here is my code :

Code: Select all

<style type="text/css">
<!--
.style2 {font-style: italic}
-->
</style>
<?
include_once 'db.inc'; 
db_connect();
$array = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$time = array ("nothing","8:00 AM","9:00 AM","10:00 AM","11:00 AM","12:00 PM","13:00 PM","14:00 PM","15:00 PM","16:00 PM","17:00 PM","18:00 PM","19:00 PM","20:00 PM");
if (isset($_POST["Preview"])){
		
		for ($x=0;$x<6;$x++){
			?>


<table width="70%" border="1">
  <tr>
    <td colspan="4"><div align="center"><strong> <? print $array[$x];?></strong></div></td>
  </tr>
  <tr>
    <td width="25%" class="style2"><div align="center">Time </div></td>
    <td width="25%"><div align="center"><em>Subject</em></div></td>
    <td width="25%"><div align="center"><em>Venue</em></div></td>
</tr>
  <?
			if (isset($HTTP_POST_VARS[$array[$x]])){
			$temp = $HTTP_POST_VARS[$array[$x]];
			$temp2 = array();
			$b = 0;
			
			?>
			

<?
			for($d=0;$d<sizeof($temp);$d++) {
			
	?>		
	<tr>
    <td width="25%"><div align="center"><? 
	
	if ($_POST[$array[$x]."Start".$temp[$d]]==$_POST[$array[$x]."End".$temp[$d]]) 
	{
	print "Error : Start and End time cannot be same! <br/>";
	print $_POST[$array[$x]."Start".$temp[$d]]." ~ ".$_POST[$array[$x]."End".$temp[$d]];
	}
	else if (substr($_POST[$array[$x]."Start".$temp[$d]],"0","2")>substr($_POST[$array[$x]."End".$temp[$d]],"0","2"))
	{
		print "Error : Start time must smaller then End time! <br/>";
		print $_POST[$array[$x]."Start".$temp[$d]]." ~ ".$_POST[$array[$x]."End".$temp[$d]];
	}
	
	else {
	print $_POST[$array[$x]."Start".$temp[$d]]." ~ ".$_POST[$array[$x]."End".$temp[$d]];
	/*$a=(substr($_POST[$array[$x]."Start".$f],"0","2")-substr($_POST[$array[$x]."End".$f],"0","2"))+substr($_POST[$array[$x]."Start".$f],"0","2")+substr($_POST[$array[$x]."End".$f],"0","2");
	*/
	/*for($b=0;$b<sizeof($temp);$b++) {
	
		if ($_POST[$array[$x]."Start".$temp[$d]]==$_POST[$array[$x]."End".$temp[$b]]){
			 if ($_POST[$array[$x]."Start".$temp[$d]]>$_POST[$array[$x]."End".$temp[$b]]){
			 	print "Error";
				break;
				}
		}else if ($_POST[$array[$x]."Start".$temp[$d]]<$_POST[$array[$x]."End".$temp[$b]])
			if ($_POST[$array[$x]."Start".$temp[$d]]>$_POST[$array[$x]."End".$temp[$b]]){
				print "Error";
				break;
			
			}
	/*$temp2[$b] = (substr($_POST[$array[$x]."Start".$temp[$b]],"0","2")-substr($_POST[$array[$x]."End".$temp[$b]],"0","2"))+substr($_POST[$array[$x]."Start".$temp[$b]],"0","2")+substr($_POST[$array[$x]."End".$temp[$b]],"0","2");
	*/
		
		
	//}
	
	/*foreach($temp2 as $te){
			for($t=1;$t<sizeof($temp2);$t++) {
			if (){
			
			}
	}*/
	
	}
	
	//for($t=0;$t<sizeof($temp);$t++) {
	//if (substr($_POST[$array[$x]."Start".$f],"0","2")<substr($_POST[$array[$x]."End".$temp[$t]],"0","2"))
	//{
	
	//if (substr($_POST[$array[$x]."End".$f],"0","2")<substr($_POST[$array[$x]."End".$temp[$t+1]],"0","2")){
	//print "Error";
	//}
	
	
	?> </div></td>
    <td width="25%"><div align="center"><? print $_POST[$array[$x]."Subject".$temp[$d]];?></div></td>
    <td width="25%"><div align="center"><?
	
	for($w=0;$w<sizeof($temp);$w++) {
		$stop1 = false;
			for($q=1;$q<sizeof($temp);$q++) {
	//print $_POST[$array[$x]."Venue".$temp[$d]];
	$stop = false;
	if ($_POST[$array[$x]."Venue".$temp[$w]]==$_POST[$array[$x]."Venue".$temp[$q]]){
		$stop = true;
		
		}
		if ($stop){
		$stop1 = true;
		break;
		}
	}
	if ($stop1){
		
		break;
		}
	 }
	 if ($stop1){
		
		print "Error";
		}?>
	 </div></td>
  
  </tr>
	<?		
			
			
			
			
			
			
			
								
		/*	 if ($_POST[$array[$x]."Start".$f]==$time[$k]&&$_POST[$array[$x]."End".$f]==$time[$k]){
				
				print "<td width='7%'>".$_POST[$array[$x]."Subject".$f]."</td>";
		
			}else if ($_POST[$array[$x]."Start".$f]==$time[$k]&&$_POST[$array[$x]."End".$f]!=$time[$k]){
				
				
				print "<td width='7%' colspan='2'>".$_POST[$array[$x]."Subject".$f]."</td>";
				
			}else {
				print "<td width='7%'>.</td>";
			}
			 
			  
			 */
			 
   	
			/*$_POST[$array[$x]."Start".$f];
			$_POST[$array[$x]."End".$f];
			$_POST[$array[$x]."Subject".$f];
			$_POST[$array[$x]."Venue".$f];*/
			
			
			
			
			
            }#end foreach

			}#end if 
		}#end for

	?>
	</table> 
	<br />
	<?	
}else {









function add_subject(){
$available = "Select * from course_subjects";
	$result = mysql_query($available) or die ("Cloud Not Execute The Query");
	 
	  while($row=mysql_fetch_array($result)){

	  echo ("<option  value=' [ ".$row['subject_id']." ] ".$row['subject_title']."'>".$row['subject_title']."</option>");
	
	}

}

function add_venue(){
	$venue = "Select * from roomvenue";
	$venueresult = mysql_query($venue) or die ("Cloud Not Execute The Query");
	 
	  while($row=mysql_fetch_array($venueresult)){

	  echo ("<option  value=".$row['room_id'].">".$row['room_id']."</option>");
	
	}

}


function addTime(){
/*print "
          <option>8:00 AM</option>
          <option>8:30 AM</option>
          <option>9:00 AM</option>
          <option>9:30 AM</option>
          <option>10:00 AM</option>
          <option>10:30 AM</option>
          <option>11:00 AM</option>
          <option>11:30 PM</option>
          <option>12:00 PM</option>
          <option>12:30 PM</option>
          <option>13:00 PM</option>
          <option>13:30 PM</option>
          <option>14:00 PM</option>
          <option>14:30 PM</option>
          <option>15:00 PM</option>
          <option>15:30 PM</option>
          <option>16:00 PM</option>
          <option>16:30 PM</option>
          <option>17:00 PM</option>
          <option>17:30 PM</option>
          <option>18:00 PM</option>
          <option>18:30 PM</option>
          <option>19:00 PM</option>
          <option>19:30 PM</option>
          <option>20:00 PM</option>";*/
		  
		  print "
          <option>08:00 AM</option>

          <option>09:00 AM</option>
         
          <option>10:00 AM</option>
         
          <option>11:00 AM</option>
       
          <option>12:00 PM</option>

          <option>13:00 PM</option>
     
          <option>14:00 PM</option>
   
          <option>15:00 PM</option>
 
          <option>16:00 PM</option>
 
          <option>17:00 PM</option>
 
          <option>18:00 PM</option>

          <option>19:00 PM</option>

          <option>20:00 PM</option>";
}
?>

<form name="form1" method="post" action="">
     <?
	for ($x=0;$x<6;$x++){
	?>
  <table width="607" border="1">
    <tr>
      <td colspan="4"><div align="center"><strong><? print $array[$x];?></strong></div></td>
    </tr>
    <tr>
      <td width="47"><div align="center"><em>Select</em></div></td>
      <td width="213"><div align="center"><em>Time</em></div></td>
      <td width="149"><div align="center"><em>Subject</em></div></td>
      <td width="170"><div align="center"><em>Venue</em></div></td>
    </tr>
<?
for ($j=0;$j<10;$j++){
?>
 	<tr>
      <td><label>
        <div align="center">
          <input type="checkbox" name="<? print $array[$x]."[]";?>" value="<? print $j;?>">
        </div>
      </label></td>
      <td><label>
        <div align="center">
          <select name="<? print $array[$x]."Start".$j;?>" id="<? print $array[$x]."Start".$j;?>">
            <? addtime(); ?>
          </select>
          ~
          <select name="<? print $array[$x]."End".$j;?>" id="<? print $array[$x]."End".$j;?>">
            <? addtime(); ?>
          </select>
        </div>
      </label></td>
      <td><label>
        <div align="center">
          <select name="<? print $array[$x]."Subject".$j;?>" id="<? print $array[$x]."Subject".$j;?>">
            <?
		add_subject();
		?>
          </select>
        </div>
      </label></td>
      <td><label>
        <div align="center">
          <select name="<? print $array[$x]."Venue".$j;?>" id="<? print $array[$x]."Venue".$j;?>">
            <?
		add_venue();
		?>
          </select>
        </div>
      </label></td>
    </tr>
	<?
	}
	?>
  </table>
  <br />
  <?
  }
  ?>
  <p>
    <label>
    <input name="Preview" type="submit" id="Preview" value="Preview">
    </label>
    <label>
    <input type="reset" name="Submit2" value="Reset">
    </label>
  </p>
</form>
<p>
  <?
}#end else isser preview
?>
</p>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I don't see where you're storing the time in the database, also like sami said 24 hour time does not use meridian.


Anyways you'll need to store the time in the database as HHmm


Something like 8:30 would become 0830, something like 14:00 would be 1400


This way you can sort and compare them in an easy manner.

Let's say timeblock A is:

8:00 - 10:00

and timeblock B is

9:00 - 100:


If start time of A is <= the start time of B
And end time of A is >= end time of B
Then there is a "conflict"

Similarly;


If start time of B is <= the start time of A
And end time of B is >= end time of A
Then there is a "conflict"

Then encapsulation


If start time of A is <= the start time of B
And end time of A is >= end time of B
Then there is a "conflict"

If start time of B is <= the start time of A
And end time of B is >= end time of A
Then there is a "conflict"



Following that logic, if each one of those tests passes with no conflicts then the times do not intervene with each other. This is all what arborint mentioned but I guess the logic didn't sink in, hope this helps.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Yeah, that's what I was suggesting but I didn't elaborate as nicely as you did, jshpro2. :P
adisonz
Forum Newbie
Posts: 11
Joined: Wed Feb 08, 2006 2:09 am

Post by adisonz »

Thank you ...everybody .

i see the logics already , i try to implement in my code .

Thank you
adisonz
Forum Newbie
Posts: 11
Joined: Wed Feb 08, 2006 2:09 am

Post by adisonz »

Yeah
thanks you

Code: Select all

function checkTime(){
global $start,$end,$array;

	
		for ($x=0;$x<sizeof($start);$x++){
					for ($y=0;$y<sizeof($start[$x]);$y++){
					if ($y==sizeof($start[$x])-1){
					$a=-1;
					}else {
					$a=1;			
					}
					if (($start[$x][$y]<=$start[$x][$y+$a])&&($end[$x][$y]>=$end[$x][$y+$a])){
					$Msg[$x][$y] = "Time Conflict On :".$array[$x]."Checked Box no : ".$y;
					
					}else {
					$Msg[$x][$y] = $start[$x][$y].":00 ~ ".$end[$x][$y].":00";
					}
					
					}
		
		}
Post Reply