兩個日期我有員工的旅行史之間。我要檢查,對於特定的月份,無論他是在分站(旅行外),或在辦公室,要計算的旅遊小時數。只要我們保持行程數據庫中,我們與旅行日期和返回日期前往客戶所輸入的員工姓名。如何檢查給定的日期期間在MySQL和PHP
一個員工有以下數據:
- 旅行日期: '2015年8月29日'(2015年8月29日)
- 返回的日期: '2015年11月6日'(2015年11月6日)
所以在這裏,我想在十月份檢查所有不在辦公室的員工。顯然這個傢伙應該進入這個類別,但我無法得到他。
我也試過直接在MySQL工作臺,但我沒有得到的結果。
我原來的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 <=?end and?start <= return_date' – shawnt00
那麼,2015/9/15不在2015/10-01和2015-10-31之間以及2015年6月11日之前'不在'2015-10-01'和'2015-10-31'之間。那爲什麼它會出現?它不符合您的查詢條件。 「 – Sean