Find best suitable time from given time interval of different users.
Rows: 5
fid userid FromDateTime ToDateTime flag
62 1 2012-07-18 01:
I have found a hacky way to do this :
Perl has some thing called Set::IntSpan
which has a intersect
function(or method) that will find a range common to two intervals of numbers. The idea is to make use of it.
You can convert date time strings to timestamp(numbers) using strtotime("2012-08-27 02:02:02")
in php. Once you have two pairs of timestamps, you can use the following sample perl code to find the intersection interval from which you can find the time.
use Set::IntSpan;
my $r1 = Set::IntSpan->new([ 5 .. 15 ]);
my $r2 = Set::IntSpan->new([ 2 .. 20 ]);
my $i = $r1->intersect($r2);
if ( !$i->empty and ( $i->max - $i->min ) >= 5 ) # criteria
{
print "hit\n"; # $i->max, $i->min are the timestamps you need
}
else
{
print "miss\n";
}
once you have the intersecting interval, you can get back the date time from timestamp (if you need) using date("Y-m-d H:i:s", $timestamp);
Here are some related links and references:
Calculate overlap between 2 ranges of numbers
Calling Perl script from PHP and passing in variables, while also using variablized perl script name
p.s. perhaps perl pros can wrap up the code into a function with 4 arguments? also, i understand this isn't the perfect answer to the question but imo the idea is cool.