1
// Listing of holidays, Down Fridays, etc... 
$sqlHol = "SELECT holidayDate FROM holidays";
$holidays = array(); 
$stmtHol = sqlsrv_query( $conn, $sqlHol );

if( $stmtHol === false ) 
{
     die( print_r( sqlsrv_errors(), true));
}               
if ( $stmtHol )
{ 
    $rowsHol = sqlsrv_has_rows( $stmtHol );
    if ($rowsHol === false)
    {
         die( print_r( sqlsrv_errors(), true));
    }
    else while( $rowsHol = sqlsrv_fetch_array( $stmtHol, SQLSRV_FETCH_ASSOC) )
    {
        $holidays[] = $rowsHol['holidayDate']; // This is where I fill my array
    }
}       

var_dump $holidays //This is the var_dump of the array $holidays
Array ( [0] => 2018-01-01 1 => 2018-01-12 [2] => 2018-01-15 [3] => 2018-01-26 [4] => 2018-02-09 [5] => 2018-02-19 [6] => 2018-02-23 [7] => 2018-03-09 [8] => 2018-03-23 [9] => 2018-04-06 [10] => 2018-04-20 [11] => 2018-05-11 [12] => 2018-05-25 [13] => 2018-05-28 [14] => 2018-06-01 [15] => 2018-06-15 [16] => 2018-06-29 [17] => 2018-07-04 [18] => 2018-07-13 [19] => 2018-07-27 [20] => 2018-08-10 [21] => 2018-08-24 [22] => 2018-09-03 [23] => 2018-09-07 [24] => 2018-09-21 [25] => 2018-10-05 [26] => 2018-10-08 [27] => 2018-10-19 [28] => 2018-11-02 [29] => 2018-11-12 [30] => 2018-11-16 [31] => 2018-11-22 [32] => 2018-11-30 [33] => 2018-12-14 [34] => 2018-12-25 [35] => 2018-12-28 [36] => 2019-01-01 [37] => 2019-01-11 [38] => 2019-01-21 [39] => 2019-01-25 [40] => 2019-02-08 [41] => 2019-02-18 [42] => 2019-02-22 [43] => 2019-03-08 [44] => 2019-03-22 [45] => 2019-04-05 [46] => 2019-04-19 [47] => 2019-05-03 [48] => 2019-05-17 [49] => 2019-05-27 [50] => 2019-05-31 [51] => 2019-06-14 [52] => 2019-06-28 [53] => 2019-07-04 [54] => 2019-07-12 [55] => 2019-07-26 [56] => 2019-08-09 [57] => 2019-08-23 [58] => 2019-09-02 [59] => 2019-09-06 [60] => 2019-09-20 [61] => 2019-10-04 [62] => 2019-10-14 [63] => 2019-10-18 [64] => 2019-11-01 [65] => 2019-11-11 [66] => 2019-11-15 [67] => 2019-11-28 [68] => 2019-11-29 [69] => 2019-12-13 [70] => 2019-12-25 [71] => 2019-12-27 [72] => 2020-01-01 [73] => 2020-01-10 [74] => 2020-01-20 [75] => 2020-01-24 [76] => 2020-02-07 [77] => 2020-02-17 [78] => 2020-02-21 [79] => 2020-03-06 [80] => 2020-03-20 [81] => 2020-04-03 [82] => 2020-04-17 [83] => 2020-05-01 [84] => 2020-05-15 [85] => 2020-05-25 [86] => 2020-05-29 [87] => 2020-06-12 [88] => 2020-06-26 [89] => 2020-07-03 [90] => 2020-07-10 [91] => 2020-07-24 [92] => 2020-08-07 [93] => 2020-08-21 [94] => 2020-09-04 [95] => 2020-09-07 [96] => 2020-09-18 [97] => 2020-10-02 [98] => 2020-10-12 [99] => 2020-10-16 [100] => 2020-10-30 [101] => 2020-11-11 [102] => 2020-11-13 [103] => 2020-11-26 [104] => 2020-11-27 [105] => 2020-12-11 [106] => 2020-12-25 [107] => 2020-12-25 )

However, the in_array used in the count5WD does not find a match to any of the values in the array when comparing $next1WD to $holidays. An example is a student starts 4-18-2018 and should complete on 4-27-2018. It needs to exclude weekends and a down Friday (4-20-2018). The result should be 7 days from the start but, I get 8 days because the down Friday is not skipped (found).

// Calculate the number of days between start and end dates
$count5WD = 0;
$temp = strtotime( $rowsBlk['startDate'] ); //example as today is 2016-03-25
$curHrs = 59.5/9;  //Total course hours/hours per day = ~7 days
while( $count5WD<$curHrs )
{
    $next1WD = strtotime( '+1 weekday', $temp );
    $next1WDDate = date( 'Y-m-d', $next1WD );
    if( in_array( $next1WDDate, $holidays ) ) //looks for $next1WD in the array
    {
        $count5WD++;
    }
    $temp = $next1WD;
}

$next5WD = date( "m/d/Y H:i:s", $temp ); //this is the outputted future date

var_dump $next1WDDate //This is the var_dump of the array $next1WDDate
2018-04-192018-04-202018-04-232018-04-242018-04-252018-04-262018-04-272018-04-302018-05-012018-05-022018-05-032018-05-042018-05-072018-05-082018-05-092018-05-102018-05-112018-05-142018-05-152018-05-162018-05-172018-05-182018-05-212018-05-222018-05-232018-05-242018-05-252018-05-282018-05-292018-05-302018-05-312018-06-012018-06-042018-06-052018-06-062018-06-072018-06-082018-06-112018-06-122018-06-132018-06-142018-06-152018-06-182018-06-192018-06-202018-06-212018-06-222018-06-252018-06-262018-06-272018-06-28

Loading the array statically (see below), the program works just fine. I believe it has something to do with the format of the data in the array but, I cannot figure it out. Comparing the array data and the $next1WD data seems to show they are the same.

$holidays = array(
    '2018-01-01',
    '2018-01-12',
    '2018-01-15',
    '2018-01-26',
    '2018-02-09',
    '2018-02-19',
    '2018-02-23',
    '2018-03-09',
    '2018-03-23',
    '2018-04-06',
    '2018-04-20',
    '2018-05-04',
    '2018-05-18',
    '2018-05-28',
    '2018-06-01',
    '2018-06-15',
    '2018-06-29',
);
Charles D
  • 11
  • 3
  • Could you include var_dumps of $next1WDDate and $holidays – Lou May 01 '18 at 14:30
  • 2
    `$holidays = array();` You're emptying the array every time through the loop (and then assigning `$holidays` to a string, meaning there _is no array_). – Patrick Q May 01 '18 at 14:30
  • is this `else while(` even possible? – Jeff May 01 '18 at 14:36
  • Please apply some formatting to the output in your update. Also, indicate where in your code you are generating that output. If you are doing `$holidays = date( "Y-m-d", $newDate );`, then I can guarantee you that the result of `var_dump($holidays);` is not what you put above. You're making it hard for us to help you as what you're showing just doesn't make sense. – Patrick Q May 01 '18 at 19:25
  • @Patrick Q I have updated the code to hopefully clear up any issues. The basics is this: I need the $holidays array to be filled dynamically, then as the count5WD code runs it looks for matching days in the $holidays array and excludes them from the count. – Charles D May 02 '18 at 01:30
  • Updated the code with my fix. Fixed the problem by changing the "$holidays = $rows;" to "$holidays[] = $rowsHol['holidayDate'];" and now it works now. @Patrick Q Thanks for the point to move the $holidays = array();. It first my first problem. – Charles D May 09 '18 at 02:26

0 Answers0