简体   繁体   中英

Why can't get the value of weekend in php

I don't know what are the errors of this code because it's suddenly not accurate the showing value to the user like this:
在此处输入图片说明

  $holidays = array();
        $query_holiday = "SELECT * FROM holiday_tbl";
         $result = mysqli_query($dbcon, $query_holiday);
         while($row_holiday = mysqli_fetch_assoc($result))
  {
     array_push($row_holiday, $holidays);
  }


if(strtotime(date("Y-m-d")) > strtotime($row['due_date']))
{

$currentDate = date("Y-m-d");
$days = (strtotime($currentDate) - strtotime($row['due_date'])) / 86400;
$daysTemp = $days;
for($i=1;$i<$days;$i++)
{
    $currentDay = date("D", strtotime("+ ".$i." days"));
    $date_loop =  date("Y-m-d", strtotime("+ ".$i." days"));
    if($currentDay == "Sun" || $currentDay == "Sat")
    {
        $daysTemp--;
    }
    else if(in_array($date_loop, $holidays))
    {
       $daysTemp--;
     }
}
echo $daysTemp;
}

The current implementation is rather convoluted. In these cases I find it's always better to start over and try to simplify the logic as much as possible. Here is my take on your problem:

function calculate_days_late($due_date = '', $holidays = array(), $current_date = 'today') {
    $days_late = 0;

    // Get the timestamps for due date and current date
    $current_date_ts = strtotime($current_date);
    $due_date_ts = strtotime($due_date);

    // If current date is not after due date then not late
    if ($current_date_ts <= $due_date_ts) {
        return $days_late;
    }

    $loop_date_ts = $current_date_ts;
    while ($loop_date_ts > $due_date_ts) {

        // If the looping date is not a weekend or holiday then count it
        if ( ! in_array(date('D', $loop_date_ts), array('Sat','Sun'))
            && ! in_array(date('Y-m-d', $loop_date_ts), $holidays)) {
            $days_late++;
        }

        $loop_date_ts = strtotime('-1 day', $loop_date_ts);
    }

    return $days_late;
}

// Test
echo '2017-09-05 = ' . calculate_days_late('2017-09-05', array(), '2017-09-05') . "\n";
echo '2017-09-06 = ' . calculate_days_late('2017-09-05', array(), '2017-09-06') . "\n";
echo '2017-09-07 = ' . calculate_days_late('2017-09-05', array(), '2017-09-07') . "\n";
echo '2017-09-08 = ' . calculate_days_late('2017-09-05', array(), '2017-09-08') . "\n";
echo '2017-09-09 = ' . calculate_days_late('2017-09-05', array(), '2017-09-09') . "\n";
echo '2017-09-10 = ' . calculate_days_late('2017-09-05', array(), '2017-09-10') . "\n";
echo '2017-09-11 = ' . calculate_days_late('2017-09-05', array(), '2017-09-11') . "\n";
echo '2017-09-12 = ' . calculate_days_late('2017-09-05', array(), '2017-09-12') . "\n";
echo '2017-09-13 = ' . calculate_days_late('2017-09-05', array(), '2017-09-13') . "\n";

Working example: https://eval.in/856018

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM