日期之间的 yii 标准条件 [英] yii criteria condition between date

查看:20
本文介绍了日期之间的 yii 标准条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找特定用户的时间重叠,我认为我的逻辑会捕获所有时间段,但这仅在开始或结束在时间范围之间时才捕获,但如果两者都是开始和结束,则未找到任何内容在一些记录时间段之间!

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

问题出在哪里?

推荐答案

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

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
)

这篇关于日期之间的 yii 标准条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆