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

时间:2015-06-17 08:50:20

标签: 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);

1 个答案:

答案 0 :(得分:1)

您可以使用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';