yii criteria condition between date

2019-01-29 12:35发布

I'm trying to find time overlaps of a specific user, I think my logic would catch all over laps, but this catches only if start OR end is in between a time frame but nothing is found if both are start and end are in between some records time period!

 $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);

Where is the problem?

标签: php yii
1条回答
虎瘦雄心在
2楼-- · 2019-01-29 13:24

Two dates are overlapping, if at least one of each one's boundaies are between the other's boundaries:

...
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
)
查看更多
登录 后发表回答