【发布时间】:2016-04-05 16:03:31
【问题描述】:
我有一个计算得出两个日期之间的劳动天数总数。但是,如果日期范围包含列表中存在的任何日期(假期),我必须从总天数中减去假期中的每一天。
这意味着如果在假期列表中我有 25-03-2016,并且日期范围从 21-03-2016 到 25-03-2016,它应该只计算 4 天,而不是 5。
目前,我在数据库中有这张假期表:
Date
01-01-2016
25-03-2016
27-06-2016
16-07-2016
15-08-2016
19-09-2016
10-10-2016
31-10-2016
01-11-2016
08-12-2016
而总的劳动天数来自这个函数:
$days=0;
$aux=strtotime($begin_date); #Aux for days
while ($aux <=strtotime($end_date)) { #While the Aux is less than the end, I verify wich day of the week is this
$auxday=date('N',$aux);
if ($auxday!='6'&&$auxday!='7') { # if is different of 6 or 7, (saturday and sunday) I add 1 to the total amount of days
$days++;
}
$aux=$aux+86400; #I add 1 day to AUX
}
更新
$sqlF="SELECT * FROM tbl000_feriados";
$requestF=mysqli_query($connect,$sqlF);
$holidays=mysqli_fetch_array($requestF);
foreach($holidays as $arr){
$holiday_arr[] = strtotime($arr);
}
$days=0;
$aux=strtotime($begin_date);
while ($aux <=strtotime($end_date)) {
if(!in_array($aux,$holiday_arr)){
$auxday=date('N',$aux);
if ($auxday!='6'&&$auxday!='7') {
$days++;
}
}
$aux=$aux+86400;
}
不减去节假日的天数
**** UPADTE 2
问题与表 tbl000_feriados 中的讲座有关,因为如果我将数据手动放入数组中
array(2016-03-25)
例如,它可以正确地进行减法。
从 tbl000_feriados 读取日期时出了点问题
$sqlF="SELECT * FROM tbl000_feriados";
$requestF=mysqli_query($connect,$sqlF);
$holidays=mysqli_fetch_array($requestF);
***** 更新 3
由于某种原因,当我从 23/03 开始计算天数并避免 28/03 时,它会提供额外的折扣
【问题讨论】:
-
如果你循环遍历日期范围并用 in_array 将每一天与列表进行比较,然后保留一个计数器?
-
实际上我遵循@Aparna 的指示,这很有意义,但是如果范围包含假期后的天数,它不会减去
-
它就像代码一样无法将假期中的天数识别为要比较的日期