doukong1897 2017-09-06 05:19
浏览 27
已采纳

为什么不能在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:
enter image description here

  $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;
}
  • 写回答

1条回答 默认 最新

  • duandaishi9268 2017-09-06 06:14
    关注

    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') . "
    ";
    echo '2017-09-06 = ' . calculate_days_late('2017-09-05', array(), '2017-09-06') . "
    ";
    echo '2017-09-07 = ' . calculate_days_late('2017-09-05', array(), '2017-09-07') . "
    ";
    echo '2017-09-08 = ' . calculate_days_late('2017-09-05', array(), '2017-09-08') . "
    ";
    echo '2017-09-09 = ' . calculate_days_late('2017-09-05', array(), '2017-09-09') . "
    ";
    echo '2017-09-10 = ' . calculate_days_late('2017-09-05', array(), '2017-09-10') . "
    ";
    echo '2017-09-11 = ' . calculate_days_late('2017-09-05', array(), '2017-09-11') . "
    ";
    echo '2017-09-12 = ' . calculate_days_late('2017-09-05', array(), '2017-09-12') . "
    ";
    echo '2017-09-13 = ' . calculate_days_late('2017-09-05', array(), '2017-09-13') . "
    ";
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路