yii日期之间的标准条件

时间:2013-10-12 12:15:45

标签: php yii

我正在尝试找到特定用户的时间重叠,我认为我的逻辑会捕获所有的圈数,但只有当开始或结束在时间范围之间时才捕获但是如果两者都是开始和结束则没有找到介于一些记录时间段之间!

 $tempModel = clone $this; // clone model

 $criteria->addCondition('
      ( t.user = :user AND (t.startDate >= :start AND t.startDate <= :end ))
      OR
      ( t.user = :user AND (t.endDate >= :start AND t.endDate <= :end ))
  ');
  $criteria->params = array(
        ':start' => date('Y-m-d', strtotime($tempModel->startDate)),
        ':end' => date('Y-m-d', strtotime($tempModel->endDate)),
        ':user ' => $tempModel->userID ,
    );

 if(!empty($tempModel->idUserTime)) // dont find updated record
            $criteria->addCondition('t.idUserTime != ' . $tempModel->idUserTime);

 $criteria->addCondition('t.active = 1'); // if this is an active user time

 $hasRecord = Usertime::model()->findAll($criteria);

问题出在哪里?

1 个答案:

答案 0 :(得分:2)

如果每个日期的边界中至少有一个在另一个边界之间,则两个日期重叠:

...
WHERE some_other_conditions AND (
    date1.start BETWEEN date2.start AND date2.end OR
    date1.end   BETWEEN date2.start AND date2.end OR
    date2.start BETWEEN date1.start AND date1.end OR
    date2.end   BETWEEN date1.start AND date1.end
)