gpt4 book ai didi

php - 通过比较mysql中的两个日期来获取数据

转载 作者:行者123 更新时间:2023-11-29 21:01:17 24 4
gpt4 key购买 nike

我每周都会获取旅行详细信息,并计算他在客户所在地花费的工作时间。下面我有一名员工从 2016 年 4 月 3 日到 2016 年 4 月 30 日外出旅行。该员工该月的可用记录只有一条。

43  3   International   Moho    Nether  2016-04-03 14:29:12 2016-04-30 14:29:12 Demo

只是我想按周检索记录。所以我尝试了一个查询,它返回某些日期的行,有时则不返回。我只是用一周的日期尝试了这三个查询。

//Last week of april (25th Apr to 1st May)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-25' and date(return_date) <= '2016-05-01'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-25' or '2016-05-01' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-25' and date(travel_date) <= '2016-05-01' ) or (date(return_date) >= '2016-04-25' and date(return_date) <= '2016-05-01')); //1 row(s) returned

//Fourth week (18th Apr to 24th Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-18' and date(return_date) <= '2016-04-24'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-18' or '2016-04-24' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-18' and date(travel_date) <= '2016-04-24' ) or (date(return_date) >= '2016-04-18' and date(return_date) <= '2016-04-24')); //0 row(s) returned

//Third week (11th Apr to 17th Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-11' and date(return_date) <= '2016-04-17'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-11' or '2016-04-17' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-11' and date(travel_date) <= '2016-04-17' ) or (date(return_date) >= '2016-04-11' and date(return_date) <= '2016-04-17')); //0 row(s) returned

//Sec week (4th Apr to 10th Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-04-04' and date(return_date) <= '2016-04-10'); //0 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-04-04' or '2016-04-10' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-04-04' and date(travel_date) <= '2016-04-10' ) or (date(return_date) >= '2016-04-04' and date(return_date) <= '2016-04-10')); //0 row(s) returned

//Firs week (28th Mar to 3rd Apr)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) >= '2016-03-28' and date(return_date) <= '2016-04-03'); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-03-28' or '2016-04-03' <= date(return_date)); //1 row(s) returned
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and ((date(travel_date) >= '2016-03-28' and date(travel_date) <= '2016-04-03' ) or (date(return_date) >= '2016-03-28' and date(return_date) <= '2016-04-03')); //1 row(s) returned

从上面的测试来看,第二个查询在所有条件下都有效。但是当我在 PHP 文件中使用它时,第二个查询对于日期(4 月 25 日到 5 月 1 日)不起作用。

PHP coding:
///////////Employee Outstation(Travel) details/////////////
$employeeTravel = new EmployeeTravelRecord();
$TravelEntryList = $employeeTravel->Find("employee = ? and (date(travel_date) <= ? and ? <= date(return_date))",array($employeeId,$start,$end));
// $TravelEntryList = $employeeTravel->Find("employee = ? and ((travel_date >= ? and travel_date <= ? ) or (return_date >= ? and return_date <= ?))",array($employeeId,$start,$end,$start,$end));
$startdate = $start;
$enddate = $end;
$TravelTime = 0;
foreach($TravelEntryList as $Travelentry){
$TraveldateArr = explode(" ",$Travelentry->travel_date);
$Traveldate = $TraveldateArr[0];
$ReturndateArr = explode(" ",$Travelentry->return_date);
$Returndate = $ReturndateArr[0];
if($startdate >= $Traveldate)
{$firstdate = $startdate;
}
else
$firstdate = $Traveldate;

if($enddate <= $Returndate )
{
$lastdate = $enddate;
}
else
$lastdate = $Returndate;

$holidays = $this->getholidays($firstdate,$lastdate);

$totalhours = $this->getWorkingDays($firstdate,$lastdate,$holidays);
$amount = $totalhours;
error_log("totalhours" . $totalhours);
$TravelTime += $amount;
}

我不知道错误在哪里,为了获得每周记录,只有两个日期(旅行日期和返回日期)哪个最好?

我按照第三个查询通过休假的起始日期和截止日期来获取员工每周的休假情况。但在那里工作得很好。这就是我选择它作为旅行记录的原因。但这里的第三个查询不适用于所有情况。

同一个第二个查询返回本周(5 月 9 日 - 5 月 15 日)的 1 行

SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-05-09' and '2016-05-15' <=  date(return_date));

我怎样才能避免这种情况?

最佳答案

您正在使用or验证 SQL 查询中的日期列的条件 ( ... <= '2016-04-25' or '2016-05-01' <= ... ),但在 PHP 脚本中您使用的是 AND而不是 or 子句 ( ... <= ? and ? <= ... )。尝试改变条件<= ? **or** ? <= (将 and 替换为 or )并检查相同内容。

TravelEntryList = $employeeTravel->Find("employee = ? 
and (date(travel_date) <= ? or ? <= date(return_date))",array($employeeId,$start,$end));

关于php - 通过比较mysql中的两个日期来获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37202340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com