简体   繁体   English

如何检查给定的日期段在MySQL和PHP中的两个日期之间

[英]How to check given date period is between two dates in MySQL and PHP

I have a travel history of the employee. 我有该员工的旅行记录。 I want to check, for the particular month, whether he is in outstation (traveled outside) or in the office, to calculate number of hours in travel. 我想检查特定月份的时间,他是在户外(出差)还是在办公室,以计算出旅行的小时数。 Just we are maintaining the travel database, in that we entered employee name with client place traveled with travel date and returned date. 只是我们要维护差旅数据库,因为我们输入了带差旅地点和回程日期的客户地点的员工姓名。

One employee have the following data: 一名员工具有以下数据:

  • Traveled date: '2015-08-29' (29th Aug 2015) 旅行日期:'2015-08-29'(2015年8月29日)
  • returned date: '2015-11-06' (6th Nov 2015) 返回日期:“ 2015-11-06”(2015年11月6日)

So here, I want to check in the month of October, all employees that are out of the office. 因此,在这里,我想检查一下十月份所有不在办公室的员工。 Obviously this guy should come in that category, but I could not get him. 显然,这个人应该属于该类别,但我无法理解他。
I also tried directly in MySQL workbench, but I didn't get the result. 我也直接在MySQL工作台中尝试过,但是没有得到结果。

My original PHP code: 我的原始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;
    }
}

But my input data doesn't retrieve that particular employee record, because both traveled date and returned date don't fall in my input dates. 但是我的输入数据不会检索该特定员工记录,因为旅行日期和返回日期都不属于我的输入日期。

MySQL Workbench: 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';

I got only the following result: 我只有以下结果:

+----+----------+---------------+----------------+----------+------------------+------------------+----------------+
| 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   |                |
+----+----------+---------------+----------------+----------+------------------+------------------+----------------+

The following record didn't get retrieved by this query: 此查询未检索以下记录:

+---+----+---------------+------+-----+-----------------+-----------------+-----------------+
| 7 | 22 | International | MOHO | XYZ | 29/8/2015 18:00 | 6/11/2015 18:00 | FOR DDS review  |
+---+----+---------------+------+-----+-----------------+-----------------+-----------------+
SELECT * FROM employeetravelrecords
WHERE
        return_date >= '2015-10-01' /* start parameter */
    and travel_date <= '2015-10-31' /* end parameter */

The logic seems a little mind-bending at first and I've even reordered the comparisons a little from my original comment above. 逻辑乍一看似乎有些曲折,我什至从上面的原始评论中对比较进行了重新排序。 Think of it this way: you need to return after the start of the range and leave before the end of the range in order to have an overlap. 这样想:您需要在范围开始之后返回而在范围结束之前离开才能重叠。

For a longer explanation and discussion you might find this page useful: TestIfDateRangesOverlap 对于更长的解释和讨论,您可能会发现此页面有用: TestIfDateRangesOverlap

SELECT '2015-08-29' < '2015-10-31' AND '2015-11-06' >= '2015-10-01' on_leave;
+----------+
| on_leave |
+----------+
|        1 |
+----------+

You can use between for get data between two dates. 您可以使用介于之间来获取两个日期之间的数据。 Here is the working example. 这是工作示例。

Here is my table structure : 这是我的表结构:

Table User 表用户

 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

And here is my mysql query : 这是我的mysql查询:

Query 询问

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'

Result of above query : 以上查询结果:

Result 结果

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

You want to find all those people who are not available for the complete duration say between T1 & T2 (and T2 > T1). 您想查找所有在整个持续时间内都不可用的人员,例如T1和T2(以及T2> T1)之间。 The query you used will only give users whose start date and return date both lie between the given interval which is not the required output. 您使用的查询将仅向用户提供其开始日期和返回日期都在给定间隔之间的用户,而该间隔不是必需的输出。

So to get the desired output you need to check for all employees who have started their journey on or before T1 and return date is on or after T2 (thus unavailable for complete interval [T1, T2]). 因此,要获得所需的输出,您需要检查所有在T1或之前开始旅程并且返回日期在T2或之后(因此在整个间隔[T1,T2]中均不可用)的所有员工。 So the query you can use is: 因此,您可以使用的查询是:

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

If you want employees who are even partially not available between the given duration then we need employees who started their travel before T1 and have any return date later than T1 (thus making them atleast unavailable for a part of the given interval): 如果您希望在给定期间内甚至部分不可用的员工,那么我们需要那些在T1之前开始旅行并且返回日期晚于T1的员工(因此使他们在给定间隔的至少一部分时间内不可用):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM