为什么这个PHP代码检查昨天是否银行假日不起作用?

时间:2016-11-27 16:08:51

标签: php arrays date

我正在尝试检查昨天是否是英国银行假日(使用已知日期数组),然后在代码中使用布尔变量newTuple*来控制是否来自RSS提要的项目显示。

由于某种原因,昨天总是被确定为银行假日,所以我的控制结构没有按预期工作。谁能看到我错在哪里?

$bankholyesterday

1 个答案:

答案 0 :(得分:1)

您可以改用in_array

$bankhols = array ("02/01/2017", "17/04/2017", "01/05/2017", "29/05/2017", "28/08/2017", "01/01/2018", "02/04/2018", "07/05/2018", "28/05/2018", "27/08/2018", "22/04/2019", "06/05/2019", "27/05/2019", "26/08/2019", "13/04/2020", "04/05/2020", "25/05/2020", "31/08/2020");

$yesterday = date('d/m/Y', strtotime("-1 days"));

if (in_array($yesterday, $bankhols)) {
//yesterday was bank holiday
} else {
//yesterday was not bank holiday
}

我测试了代码并且它有效。我尝试在数组$ bankhols中添加昨日日期,并且它返回true,虽然它不在数组中,但它返回false。

编写了一个简短的脚本来转换和比较两个日期,因为我不知道任何替代方案:

function compareDates($date1, $date2) {
    // date1 format = d/m/Y
    // date2 format = m/d/Y

    $d3 = explode("/", $date1);
    $date1 = $d3[1] . "/" . $d3[0] . "/" . $d3[2];

    return $date1 == $date2;
}

if (compareDates("27/11/2016", "11/27/2016")) {
    echo "dates match";
} else {
    echo "dates don't match";
}