我想总结日期差异,一切都很好,但是如果有相同的日期我想加1,例如,如果起始日期是:01/01/2003 并且到目前为止是01/01/2003然后我想要添加1天,但它不会增加1天,而是只增加一天,如果是01/01/03到01/02/2003。这是计算休假,如果有人申请休假一天,即2003年1月1日至2003年1月1日,它应计算1天
$sql = "SELECT l.id as typeid,l.*,ud.firstname, ud.lastname,ud.email,l.leave_starting_from,l.leave_partial_from,ud.joining_date, l.typename, l.no_of_leave_days,(CASE SUM(datediff(STR_TO_DATE(lr.to_date, '%m/%d/%Y'), STR_TO_DATE(lr.from_date, '%m/%d/%Y'))) when NULL then '0' else SUM(datediff(STR_TO_DATE(lr.to_date, '%m/%d/%Y'), STR_TO_DATE(lr.from_date, '%m/%d/%Y'))) end) as days_in_betw, lr.from_date, lr.to_date FROM pr_users u INNER JOIN pr_users_details ud on ud.userid= u.id LEFT OUTER JOIN pr_leave_type l ON ((FIND_IN_SET( u.departmentid , l.function ) >0) AND (FIND_IN_SET( ud.designation_id , l.level ) >0) and (l.gender='All' or l.gender=ud.gender)) LEFT OUTER JOIN pr_leave_request lr ON u.id = lr.userid and lr.sr_type = l.id";

特别是其他部分
SUM(datediff(STR_TO_DATE(lr.to_date, '%m/%d/%Y'), STR_TO_DATE(lr.from_date, '%m/%d/%Y'))

答案 0 :(得分:0)
我遇到过同样的问题,愿这个功能解决你的问题。
function days_calculation($start_date,$end_date){
$start = new DateTime($start_date);
$end = new DateTime($end_date);
// otherwise the end date is excluded (bug?)
$end->modify('+1 day');
$interval = $end->diff($start);
// total days
$days = $interval->days;
// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
// best stored as array, so you can add more than one
$holidays = array();
foreach($period as $dt) {
$curr = $dt->format('D');
// substract if Saturday or Sunday
if ($curr == 'Sat' || $curr == 'Sun') {
$days--;
}
// (optional) for the updated question
elseif (in_array($dt->format('Y-m-d'), $holidays)) {
$days--;
}
}
if($days==1){
$days = $days." day";
} else {
$days = $days." days";
}
return $days;
}
答案 1 :(得分:0)
我认为这可能会有所帮助。它在T-SQL中,因此可能需要重新调整。
/* Setting up your problem */
create table #temp
(
personID int,
from_date datetime,
to_date datetime
)
insert into #temp values (1,'01/01/2003','01/01/2003')
insert into #temp values (1,'01/01/2013','05/01/2013')
insert into #temp values (2,'01/01/2013','05/01/2013')
/* Here the nested statement calculates the number of days off, then the outer select group this per personID */
select personID
, SUM(DaysOff) as TotalDaysOff
from (
select personID
, datediff(day, lr.from_date, lr.to_date)+1 as DaysOff
from #temp lr
) lr
group by personID
您需要将其更改为MySQL,然后将其放在您拥有的select语句中。