【问题标题】:How to check given date period is between two dates in MySQL and PHP如何检查给定的日期期间在 MySQL 和 PHP 中的两个日期之间
【发布时间】:2016-03-23 04:49:57
【问题描述】:

我有该员工的旅行记录。我想检查特定月份的他是在外站(在外面旅行)还是在办公室,以计算旅行小时数。只是我们正在维护旅行数据库,因为我们输入了员工姓名,客户地点旅行,旅行日期和返回日期。

一名员工拥有以下数据:

  • 出行日期:“2015-08-29”(2015 年 8 月 29 日)
  • 返回日期:'2015-11-06'(2015 年 11 月 6 日)

所以在这里,我想在 10 月份检查所有不在办公室的员工。显然这个人应该属于那个类别,但我找不到他。
我也直接在 MySQL workbench 中试了一下,没有得到结果。

我原来的 PHP 代码:

// $req['date_start'] = '2015-10-01'
// $req['date_end'] = '2015-10-31'

$employeeTravel = new EmployeeTravelRecord();
$TravelEntryList = $employeeTravel->Find("(travel_date between ? and ? or  return_date between ? and ? )",array($req['date_start'], $req['date_end'],$req['date_start'], $req['date_end']));
$startdate = $req['date_start'];
$enddate = $req['date_end'];
foreach($TravelEntryList as $Travelentry){
    $key = $Travelentry->employee;  

    if($startdate >= $Travelentry->travel_date)
    {
        $firstdate = $startdate;
    }
    else
        $firstdate = $Travelentry->travel_date;

    if($enddate <= $Travelentry->return_date )
    {
        $lastdate = $enddate;
    }
    else
        $lastdate = $Travelentry->return_date;

    $holidays = $this->getholidays($firstdate,$lastdate);
    $totalhours = $this->getWorkingDays($firstdate,$lastdate,$holidays); //It returns in total time of outstation in hours excluding company holidays
    $amount = $totalhours;
    if(isset($TravelTimeArray[$key])){
        $TravelTimeArray[$key] += $amount;
    }else{
        $TravelTimeArray[$key] = $amount;
    }
}

但是我的输入数据没有检索到那个特定的员工记录,因为旅行日期和返回日期都不属于我的输入日期。

MySQL 工作台:

SELECT * FROM employeetravelrecords where travel_date between '2015-10-01' and '2015-10-31' or  return_date between '2015-10-01' and '2015-10-31';

我只得到以下结果:

+----+----------+---------------+----------------+----------+------------------+------------------+----------------+
| id | employee | type          | project        | place    | travel date      | return date      | details        |
+----+----------+---------------+----------------+----------+------------------+------------------+----------------+
| 13 | 38       | International | PVMTC Training | Hongkong | 11/10/2015 13:33 | 28/11/2015 13:33 | PVMTC Training |
| 14 | 48       | International | PVMT           | VIETNAM  | 10/10/2015 9:28  | 1/1/2016 9:28    | PETRO          |
| 17 | 57       | International | PVMT           | Saudi    | 10/10/2015 11:39 | 2/1/2016 11:39   |                |
+----+----------+---------------+----------------+----------+------------------+------------------+----------------+

此查询未检索到以下记录:

+---+----+---------------+------+-----+-----------------+-----------------+-----------------+
| 7 | 22 | International | MOHO | XYZ | 29/8/2015 18:00 | 6/11/2015 18:00 | FOR DDS review  |
+---+----+---------------+------+-----+-----------------+-----------------+-----------------+

【问题讨论】:

  • 也包括 employeetravelrecords 架构。
  • 您需要测试重叠。您所写的条件要求它实际上在 10 月开始或结束。 travel_date &lt;= ?end and ?start &lt;= return_date
  • 嗯,29/8/2015 不在'2015-10-01' and '2015-10-31'6/11/2015 之间,不在'2015-10-01' and '2015-10-31' 之间。那么它为什么要出现呢?它不符合您的查询条件。
  • "28/11/2015 13:33" 看起来不像是日期数据类型。那里发生了什么?
  • 我将数据从mysql复制到excel。所以日期列的格式是这样的......就是这样。

标签: php mysql


【解决方案1】:
SELECT * FROM employeetravelrecords
WHERE
        return_date >= '2015-10-01' /* start parameter */
    and travel_date <= '2015-10-31' /* end parameter */

起初逻辑似乎有点令人费解,我什至根据上面的原始评论对比较进行了一些重新排序。可以这样想:您需要在范围开始之后返回 并在范围结束之前离开 之前 以便有重叠。

要获得更长的解释和讨论,您可能会发现此页面很有用:TestIfDateRangesOverlap

【讨论】:

  • 我怀疑其中一个不应该是 '='。
【解决方案2】:
SELECT '2015-08-29' < '2015-10-31' AND '2015-11-06' >= '2015-10-01' on_leave;
+----------+
| on_leave |
+----------+
|        1 |
+----------+

【讨论】:

    【解决方案3】:

    您可以使用 between 获取两个日期之间的数据。这是工作示例。

    这是我的表结构:

    表用户

     user_id    user_name   created_date            modified_date
     1          lalji nakum 2016-01-28 17:07:06     2016-03-31 00:00:00
     2          admin       2016-01-28 17:25:38     2016-02-29 00:00:00
    

    这是我的 mysql 查询:

    查询

    SELECT * FROM `user` WHERE created_date between '2015-12-01' and '2016-12-31' or modified_date between '2015-12-01' and '2016-12-31'
    

    以上查询结果:

    结果

    user_id     user_name   created_date            modified_date
     1          lalji nakum 2016-01-28 17:07:06     2016-03-31 00:00:00
     2          admin       2016-01-28 17:25:38     2016-02-29 00:00:00
    

    【讨论】:

    • 实际上你可以在我的问题中看到我使用了和你一样的查询。而且对于您的数据,您是否尝试在“2016-02-01”和“2016-02-28”之间取值。因为我尝试使用您的数据,但它不起作用。请检查您的身边。
    • 您给出的日期表中没有数据。所以它会返回空结果。
    【解决方案4】:

    您想找到所有在 T1 和 T2 之间(以及 T2 > T1)期间没有空的人。您使用的查询只会为开始日期和返回日期都位于给定间隔之间的用户提供不是必需的输出。

    因此,要获得所需的输出,您需要检查在 T1 或之前开始旅程并且返回日期在 T2 或之后的所有员工(因此对于完整的时间间隔 [T1,T2] 不可用)。所以你可以使用的查询是:

    SELECT * FROM employeetravelrecords where travel_date <= '2015-10-01' and return_date >= '2015-10-31';
    

    如果您想要在给定时间段内部分不可用的员工,那么我们需要在 T1 之前开始旅行并且任何返回日期晚于 T1 的员工(从而使他们至少在给定时间间隔的一部分内不可用):

    SELECT * FROM employeetravelrecords where travel_date <= '2015-10-01' and return_date > '2015-10-01';
    

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多