Ben son zamanlarda bir sürü tarih çarpışma sorunları ile uğraşmak zorunda kalmıştım, ve ben ile gelmek mümkün oldum en iyisi:
date1.start < date2.end ve date1.end> date2.start = çarpışma
Bu basit formül aşağıdaki durumlardan tüm hesap verecek:
--same for all situations:
date1.start = 1/1/2009
date1.end = 1/10/2009
--Start date is in first dates range:
date2.start = 1/9/2009
date2.end = 2/10/2009
--End date is in first dates range:
date2.start = 12/10/2008
date2.end = 1/3/2009
--Start & End date is inside first dates range:
date2.start = 1/2/2009
date2.end = 1/3/2009
--First date is inside of second dates range:
date2.start = 12/1/2008
date2.end = 2/1/2009
$date1 = array('start' => '2009-01-05', 'end' => '2009-01-10');
$date2 = array('start' => '2009-01-01', 'end' => '2009-01-04'); // end inside one
$date3 = array('start' => '2009-01-04', 'end' => '2009-01-15'); // start inside one
$date4 = array('start' => '2009-01-01', 'end' => '2009-01-15'); // one inside me
$date5 = array('start' => '2009-01-04', 'end' => '2009-01-05'); // inside one
function datesCollide($date1, $date2)
{
$start1TS = strtotime($date1['start']);
$end1TS = strtotime($date1['end']);
$start2TS = strtotime($date2['start']);
$end2TS = strtotime($date2['end']);
if ($start1TS <= $end2TS && $end1TS >= $start2)
{
return true;
}
return false;
}
Based on your comment this is probably the solution you are looking for:
Note that this solution isn't very optimized, and should only be used for figuring out a better solution. Never trust code from the web.
$events = array(
array(
"id" => 21,
"start" => 1242219600,
"end" => 1242237600,
"title" => "foo",
"split" => false
),
array(
"id" => 22,
"start" => 1242223200,
"end" => 1242234000,
"title" => "foo",
"split" => false
)
);
foreach ($events as $key => $event)
{
$events[$key]->split = dateCollisionCheck($event, $events);
}
function dateCollisionCheck(&$event, &$eventList)
{
foreach ($eventList as $checkEvent)
{
if ($event->id != $checkEvent->id)
{
if ($event->start <= $checkEvent->end && $event->end >= $checkEvent->start)
{
return true; // return as soon as we know there is a collision
}
}
}
return false;
}
* Kod test edilmemiştir