0

I have a array of given elements:

date_default_timezone_set("UTC");
$DateNow = date("d/m/Y H:i", time());
$tommorowUnix = strtotime("+1 day");
$tommorowAfterUnix = strtotime("+2 day");
$holidays = array("30 October 2021",
"31 October 2021",
"01 November 2021",
"02 November 2021",
"11 November 2021",
"12 October 2021",
"25 December 2021",
"26 December 2021",
"27 December 2021",
"1 January 2022",
"2 January 2022");

And then, I had a function, to compare given $date with array, and return if found true:


foreach($holidays as &$value)
{
    $value = strtotime($value);
}

function isNextDayWeekend($date)
{
    $weekDay = date('w', $date);
    echo($weekDay);
    if($weekDay == 0 || $weekDay == 6)
        return true;
    else
        return false;   
}

function isNextDayHoliday($date)
{
    $returnVal = false;
    foreach ($holidays as $holidayDay)
    {
        echo ("test");
        if($date == $holidayDay) { $returnVal = true; }     
    }
    return $returnVal;
}
$check1 = isNextDayHoliday(strtotime("12 October 2021"));
echo $check1 ? 'true' : 'false';

Unfortunately, even the echo ("test") is not displayed.

@EDIT: Solved out above case. $holidays scope was an issue. Nonetheless, it's still giving me bad values:

function isNextDayHoliday($date,$holidays)
{
    $returnVal = false;
    foreach ($holidays as $holidayDay)
    {
        if($date == $holidayDay) {$returnval = true;}   
        echo "Checking ".$date." vs. ".$holidayDay." = ";
        echo $returnVal ? "true" : "false"."<br/>";
    }
    return $returnVal;
}
$check1 = isNextDayHoliday(strtotime("12 October 2021"),$holidays);
echo $check1 ? "true" : "false";
Checking 1633996800 vs. 1635552000 = false
Checking 1633996800 vs. 1635638400 = false
Checking 1633996800 vs. 1635724800 = false
Checking 1633996800 vs. 1635811200 = false
Checking 1633996800 vs. 1636588800 = false
**Checking 1633996800 vs. 1633996800 = false**
Checking 1633996800 vs. 1640390400 = false
Checking 1633996800 vs. 1640476800 = false
Checking 1633996800 vs. 1640563200 = false
Checking 1633996800 vs. 1640995200 = false
Checking 1633996800 vs. 1641081600 = false
false
  • `$holidays` is empty inside `function isNextDayHoliday`, its not defined inside the function. This might help: [PHP function use variable from outside](https://stackoverflow.com/questions/11086773/php-function-use-variable-from-outside) – Definitely not Rafal Jul 05 '21 at 10:30

1 Answers1

0
function isNextDayHoliday($date)
{
    $returnVal = false;
    foreach ($holidays as $holidayDay)
    {
        echo ("test");
        if($date == $holidayDay) { $returnVal = true; }     
    }
    return $returnVal;
}

You need to pass $holidays to this function, this function doesn't know what holidays is.

You can declare it globally and then can use it.

You to optimize it a bit. if($date == $holidayDay) return true; so that processing is stopped as soon as you have found it.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • your first param is `strtotime("12 October 2021")` which is unix timestamp while your holidays array has strings.. @MateuszŻymła – Danyal Sandeelo Jul 05 '21 at 10:46
  • I guess it's another issue of the scope, this time foreach loop: foreach($holidays as &$value) {$value = strtotime($value);} because reference should edit original array? – Mateusz Żymła Jul 05 '21 at 10:48
  • I can see, it's printing the values. I tested the above method and called it as `echo isNextDayHoliday("2 January 2022", $holidays);` and it returned true @MateuszŻymła – Danyal Sandeelo Jul 05 '21 at 10:48
  • copy paste this @MateuszŻymła ` "; echo isNextDayHoliday("2 January 2022", $holidays);` – Danyal Sandeelo Jul 05 '21 at 10:50
  • I even tried add $holidays = array(strtotime("30 October 2021"), strtotime("31 October 2021"), strtotime("01 November 2021"), and so on (even though it's against DRY concept), and it still doesn't work. – Mateusz Żymła Jul 05 '21 at 10:53
  • No, printing here is once again for debugging purposes. It has to compare given date against array of dates. – Mateusz Żymła Jul 05 '21 at 10:58
  • @MateuszŻymła did you copy paste the code that I shared in comment and run it? – Danyal Sandeelo Jul 05 '21 at 10:59
  • Alright, $holidays = array("30 October 2021", "31 October 2021", "01 November 2021", "02 November 2021", "11 November 2021", "12 October 2021", "25 December 2021", "26 December 2021", "27 December 2021", "1 January 2022", "2 January 2022"); foreach($holidays as &$holidayDay) { $holidayDay = strtotime($holidayDay); } function isNextDayHoliday($date, $holidays) { $returnVal = false; foreach ($holidays as $holidayDay) { echo ("test"); if($date == $holidayDay) {return true;} } return $returnVal; } returns true now correctly. – Mateusz Żymła Jul 05 '21 at 11:01