跳过几天,几个月和几周之间的周末,循环使用php


Skip Weekends between days, months and weeks in looping with php

我要求用户输入开始日期和结束日期以生成付款计划,我在两个日期内循环以根据用户选择生成每周,每天和每月的时间表。

   $f_repayment_date = "2015-01-01";
   $mat_date = "2015-10-01";
    while (strtotime($f_repayment_date) < strtotime($mat_date)) {
        $f_repayment_date = date ("Y-m-d", strtotime("+1 month", strtotime($f_repayment_date)));
      $count++;

      //calculate interest on outstanding balance

      if ($balance < $payment) {
          $payment = $balance;
          $interest_amount = $interest_amount ;
          $principal = $payment - $interest_amount;
           $due = $payment + $interest_amount;
       }
      $balance = $balance - $payment;
       if ($balance < 0) {
         $principal = $principal + $balance;
         $interest_amount  =  $interest_amount;
         $balance   = 0;
      } 
     $sql = mysql_query("insert into loan_schedule values (NULL,'$f_repayment_date','$account_number','$contract_code','$loan_product',
     '$loan_amount','$frequency','$loan_term','$interest','$payment','$principal','$interest_amount','$balance','$due','$count','','$due','$active_loan')");
       }  
    } while ($balance > 0);

您可以使用date函数:

$weekends = array(6, 7);
$day = date('N', strtotime('2015-10-01'));
if (in_array($day, $weekends)) {
    continue; // skip this day
}

您还应该将日期放在引号中,否则您将获得2013而不是'2015-01-01'

$f_repayment_date = '2015-01-01';
$mat_date = '2015-10-01';