Im getting travel details week by week and going to calculate working hours he spent in client place.Below I have an employee who travelled outside from 03Apr2016 to 30th Apr 2016.Available record for that month for that employee is only one.
43 3 International Moho Nether 2016-04-03 14:29:12 2016-04-30 14:29:12 Demo
Just I want to retrieve records by week by week. So I tried one query, it returns row for certain dates and sometimes doesn't. Just I tried these three queries with the dates of the week.
//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
From the above testing, the second query is working in all conditions. But still when I use it in my PHP file, the 2nd query didn't work for the date (25th Apr to 01st may ).
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;
}
I don't know where is the mistake and in order to get week by week record, by having only two dates (travel and return date) which one is best?
I followed the third query for getting leaves taken by the employee week by week by having From and To date of leaves. But it works fine there. That's why I chose it for travel records. But here 3rd query not working for all cases.
The same 2nd query returns 1 row for the week (09May - 15th may)
SELECT * FROM simhrmdb.employeetravelrecords where employee = 3 and (date(travel_date) <= '2016-05-09' and '2016-05-15' <= date(return_date));
How can i avoid that??
You are using or
condition for validating your date columns in your SQL query ( ... <= '2016-04-25' or '2016-05-01' <= ...
), but in PHP script your are using AND
instead of the or clause ( ... <= ? and ? <= ...
). Try to change the condition <= ? **or** ? <=
<= ? **or** ? <=
<= ? **or** ? <=
(replace the and
with or
) and check the same.
TravelEntryList = $employeeTravel->Find("employee = ?
and (date(travel_date) <= ? or ? <= date(return_date))",array($employeeId,$start,$end));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.