views:

115

answers:

3

I have 3 date / time input boxes on a form and the user cannot select any times within half an hour of each other.

I've converted all values to epoch format using a Javascript implementation of strtotime, but not sure how to recursively check that any of the times are actually half an hour apart.

I can hand code all checks, but it would be cleaner to write a recursive function (especially if there were theoretically more than 3 time slots).

Did some Google research but no luck.

Any suggestions on implementing this in Javascript or Jquery.

Thanks.

+1  A: 

Pseudo code...

epochArrayOfTimes.sort();
for (i = 0, k = 1; k <= epochArrayOfTimes.size(); i++, k++) {
    if ( (epochArrayOfTimes[i] - epochArrayOfTimes[k]) <= 30 minutes) { alert/error }
}
Nicki
This is exactly what he's asking to avoid.
SLaks
This doesn't work if time 3 is before time 2... it would work if they were sorted into an array in order of epoch time though.It solves the problem for 3 boxes, but it would be a nightmare for lots :-)
niggles
Since this is returning the absolute value ... yes, it would work
Nicki
you don't need the Math.abs anymore
Jimmy
@Jimmy, good point. Just updated.
Nicki
+5  A: 

Sort the times, then look at adjacent members and check whether they're within 30 minutes (1800 seconds) of each other.

EDIT: I hesitate to bother posting example code at all, but if your times are in an array named times:

times.sort();
for (i = 0; i < times.length-1, i++) {
  if (times[i+1] - times[i] < 1800) {
    return false;
  }
  return true;
}
jemfinch
A: 

First I want to say, I'm no good at JavaScript. So if this is wrong please don't down vote me into oblivion.

JavaScript:

var time = ["1270690925", "1270696925", "1273696925"];
var spaceTime = 60 * 30;

for (var i in time)
{
    for (var j in time)
    {
        if (time[i] - time[j] < spaceTime)
            alert("You must put a greater length between these two times: " + time[i] + " and " + time[j] + ".");
    }
}

Based off this PHP code:

function timeCheck($timeArrays = array(), $spaceTime = 1800)
{
    foreach ($timeArray as $time1)
    {
        foreach ($timeArray as $time2)
        {
            if ($time1 - $time < $spaceTime)
            {
                # These items are not appropriately spaced.
            }
        }
    }
}

Attempt #2 (After reviewing comments) in PHP:

function timeCheck($times = array(), $interval = 1800)
{
    sort($times);
    for ($i = 0, $j = 1, $k = count($times); $j < $k; ++$i, ++$j)
    {
        if (($times[$j] - $times[$i]) < $interval)
        {
            echo "{$times[$i]} is less then $interval away from {$times[$j]}." . PHP_EOL;
        }
    }
}
Mark Tomlin
There's no need for a quadratic algorithm here. Sort the array first and you only need compare adjacent elements. It's `O(n log n)` instead of `O(n^2)`.
jemfinch
Another problem with doing it this way is that, since you are comparing every time in the array to every other time in the array *and itself*, you will have cases where `time[j]` is greater than `time[i]` and `i === j`. In both of those cases, the subtraction will always be less than `spaceTime`. In the first case, it will be negative, and in the second case, it will be zero.
Matthew Crumley
@jemfinch, very interesting. I had never really considered that I was just looking for a solution and that was the first thing that came to my mind. I figured he would optimize it later. @Matthew, yeah that was my bad. I should of absolute'd it and or sorted it like the first comment said.
Mark Tomlin