[Open Source] Time Comparing Algorithm

Discussion in 'Programming' started by Adventis, Aug 30, 2007.

  1. #1
    I needed to write an algorithm that would actively compare the times of two bands for a festival. It would check to see if Band A's ending time would clash with Band B's starting time. In addition to this it would also check to see the reverse - it'd make sense if you coded the algorithm too.

    The funny thing is, I'm not a bad programmer; got a bit of experience behind me and I actually found this algorithm quite challenging. You can view my result here, and the code below. I'm posting it hoping it'll help someone else; perhaps you might want to advance on it and make some kind of scheduling application with it.

    It's not often you get something for nothing on these forums, so why don't you share some other algorithms you've made that you found challenging too? Make a bit of a habit of the open source movement ;)

    $qry1	= "SELECT * FROM Band WHERE Band = '$_POST[Band1]'";
    $res1	= mysql_query($qry1) or print mysql_error();
    $res1	= mysql_fetch_array($res1);
    
    $qry2	= "SELECT * FROM Band WHERE Band = '$_POST[Band2]'";
    $res2	= mysql_query($qry2) or print mysql_error();
    $res2	= mysql_fetch_array($res2);
    
    if($res2['Day'] == $res1['Day'])
    {
    	
    	$startOfA	= $res1['TimeStart'];
    	$endOfA		= $res1['TimeEnd'];
    	$startOfB	= $res2['TimeStart'];
    	$endOfB		= $res2['TimeEnd'];
    	
    	// text output
    	if($startOfB >= $startOfA)
    	{
    	  if($endOfA > $startOfB)
    	  {
    		echo($_POST['Band1']." <b>will clash</b> with  ".$_POST['Band2']);
    	  }
    	  else
    	  {
    		echo($_POST['Band1']." <b>won't clash</b> with  ".$_POST['Band2']);
    	  }
    	}
    	else
    	{
    	  if($endOfB > $startOfA)
    	  {
    		echo($_POST['Band1']." <b>will clash</b> with ".$_POST['Band2']);
    	  }
    	  else
    	  {
    		echo($_POST['Band1']." <b>won't clash</b> with ".$_POST['Band2']);
    	  }
    	}
    	
    }
    else
    {
    	echo("Two different days");
    }
    
    Code (markup):
    While I realise this doesn't come across as amazingly challenging - nothing more than some greater thans and a bit of logic, I recommend you try to code a similar algorithm using a drop down list of bands participating on the day. You might find it quite puzzling too! :)
     
    Adventis, Aug 30, 2007 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Having programmed a staff holiday booking system with business rules (authorise automatically if less than 3 on holiday, prompt if 3, escalate for auth if over 3) it is very difficult to do these types of things. Took a lot of thinking about the logic - thankfully you are only comparing 2 people/ bands for a short time.
     
    AstarothSolutions, Aug 30, 2007 IP
  3. NoobieDoobieDo

    NoobieDoobieDo Peon

    Messages:
    1,456
    Likes Received:
    53
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I would highly suggest pen + paper. It's a little old school but it still works.

    ;)
     
    NoobieDoobieDo, Aug 30, 2007 IP
  4. Adventis

    Adventis Guest

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah that's how I sussed it. 3 A4's pages and two failed trace tables later.. :D
     
    Adventis, Aug 30, 2007 IP
  5. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #5
    ok here goes, and forgive me if i have missed something:

    i store times / dates as unix time always, if not you can convert dates into unixtime using UNIXTIME() [mysql]

    so basically i'd do something like this

    
    SELECT band1.gig_id
    FROM gigs AS band1
    LEFT JOIN gigs AS band2 ON gig_bandid = $_POST['band2']
    WHERE band1.gig_bandid = $_POST['band1'] 
               AND band1.gig_finishtime > band2.starttime
               AND band1.gig_starttime < band2.starttime
    
    Code (markup):
    What this will do is bring up the first bands gig, join the same table again for information about the second band, itll then check to see if the second gig starts before the first gig has finished, if so itll return a row. So you need to check that no rows are returned from the query to see if it "passed"

    you may want to add extra conditions with regards to the gig id's etc, but that should give you the base

    hope it helps
     
    m0nkeymafia, Aug 31, 2007 IP
  6. Adventis

    Adventis Guest

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ah, yes. I was always going to consider doing join queries; I suppose it would have been better to in retrospect.
     
    Adventis, Sep 3, 2007 IP