如果日期范围包含PHP列表中的日期,则减去天数

时间:2016-04-05 16:03:31

标签: php mysql date

我有一个计算,给出了2个日期之间的实验日总数。但是,如果日期范围包含列表中的任何日期(假期),我必须从节假日中减去每天的总天数。

这意味着如果在假日列表中我有25-03-2016,并且日期范围从2016年3月21日到2016年3月25日,它应该只计算4天,而不是5。 / p>

目前,我在DB中有这个假期表:

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)
例如,它正确地减去了。

从pbl000_feriados

读取日期会出现问题
$sqlF="SELECT * FROM tbl000_feriados";
$requestF=mysqli_query($connect,$sqlF);
$holidays=mysqli_fetch_array($requestF);

*****更新3

出于某种原因,当我计算从23/03和avobe 28/03开始计算的天数时,它会提供额外的折扣

3 个答案:

答案 0 :(得分:1)

您可以使用mysql BETWEEN运算符查找给定日期(在您的情况下为$ begin_date和$ end_date)之间的假期数:

SELECT COUNT(*) from holidays where Date between Date('2016-02-05') and date('2016-04-01');

只要数据库中的Date是数据库中的datedatetime类型,mysql就会知道如何处理它。但请注意,您的欧洲日期格式(dd-mm-yyyy)虽然可以说比愚蠢的美国mm-dd-yyyy格式更合理,但可能无法正确测试日期。我强烈推荐更普遍一致的YYYY-MM-DD格式(它具有时间顺序与字典顺序相匹配的额外好处,这意味着字母顺序日期列表也将按时间顺序排列!)

将日期保留为数据库中的日期是您可以做的最明智的事情之一。您不仅可以进行这样的以日期为中心的比较,而且您也可以开始考虑支持 - 或者至少明确设置 - 时区。我提醒日期相关程序的所有开发人员使用显式时区启动。如果您将日期放入表中但没有应用正确的时区,那么当您稍后尝试处理时区并且数据未作为mysql服务器默认运行的时区放入时,可能很难清理。恶心!

答案 1 :(得分:0)

首先,通过将日期转换为strtotime格式,在假日列表中创建日期数组。

$days=0;
$holidays = array('2016-01-01', '2016-03-25');
foreach($holidays as $arr){
  $holiday_arr[] = strtotime($arr);
}
$begin_date = '2016-01-01';
$end_date = '2017-01-01';
$aux=strtotime($begin_date); #Aux for days
while ($aux <=strtotime($end_date)) { 

然后将它与新数组进行比较,如果假日列表数组中不存在此日期,则从此处开始

if(!in_array($aux,$holiday_arr)){

虽然Aux不到最后,但我确认一周的哪一天是

    $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
}

答案 2 :(得分:0)

尝试以下方法:

$holidays = array('2016-01-01', '2016-03-25');

$begin_date = new DateTime('2016-01-01');
$end_date = new DateTime('2017-01-01');
$exclude = 0;

// Loop over holidays to find out whether they are in the range
foreach ($holidays as $holiday) {
    if (isWithinDates(new DateTime($holiday), $begin_date, $end_date)) {
        $exclude++;
    }
}

function isWithinDates(DateTime $date, DateTime $start, DateTime $end)
{
    return $date >= $start && $date <= $end;
}

// Get the amount of days between our start date and the end date
// and subtract the amount of days that we know to be holidays
$interval = $begin_date->diff($end_date)->days - $exclude;

var_dump($interval);