如何根据条件添加日期数据?

时间:2016-09-16 11:35:23

标签: php

以下是我的PHP代码,它返回日期的金额,直到条件为假:

<?php
$paid = 2000;
$monthly = 750;
$date_array = array("2016-07", "2016-09");
$bill_paid_array =  array(750, 600);

$x=0;
do {

    $found = false;
    while (!$found) {
        $date = date('2016'.'-'.'07'.'-d');
        $time = strtotime($date);
        $bill_month = date("Y-m", strtotime( ($x++)." month", $time));
        $found = !in_array($bill_month,$date_array) ;
    }

    echo date($bill_month."-d") . "=> $monthly<br/>\n";

    $paid -= $monthly;
    if ($paid < $monthly) {
        $monthly = $paid;
    }
} while ($paid > 0);

返回数据:

2016-08-16=> 750
2016-10-16=> 750
2016-11-16=> 500

工作原理:

此代码检查用户已从开始日期支付的每月帐单。如果月份存在,则显示下个月,直到条件为假。

现在适用于:例如:开始日期为:2016-07-01,用户已付费2016-072016-09月帐单(请参阅$ date_array)

现在,用户正在支付2000,其帐单金额为750。因此该代码显示以下日期与金额。

返回数据:

2016-08-16=> 750
2016-10-16=> 750
2016-11-16=> 500

我现在想要什么

现在,如果我再次支付新帐单750,则根据上述代码,此行:date($bill_month."-d") . "=> $monthly<br/>\n";将显示2016-12-16日期:但我不想要。我想展示:2016-11-16 2502016-12-16 500

注意:我每个月都有$ bill_paid_array

更新:

$allBillMonth = mysqli_query($conn, "SELECT YEAR(bill_month) AS bill_year, MONTH(bill_month) AS bill_month, pay_amount FROM clients_pay_bill WHERE client_id = '$client_id' AND uid = '$uid' ");

$date_array = array();
$pay_amount_array = array();

while( $fetchAllBillMonth = mysqli_fetch_array($allBillMonth) ) {
    $year = $fetchAllBillMonth['bill_year'];
    $month = $fetchAllBillMonth['bill_month'];
    $date_array[] = "$year-$month";
    if($monthly_bill != $fetchAllBillMonth['pay_amount']) {
        $flag = 0;
    } else {
        $flag =1;
    }

    $db_paid_amount = $fetchAllBillMonth['pay_amount'];
    $pay_amount_array[] = "$year-$month => [$db_paid_amount, $flag]";
}

输出

Array
(
    [0] => 2016-8 => [750.00, 1]
    [1] => 2016-9 => [750.00, 1]
    [2] => 2016-10 => [700.00, 0]
)

3 个答案:

答案 0 :(得分:1)

检查这个,我修改了$ bill_paid_array,如果你有任何问题我会解释

<?php
$paid = 2000;
$monthly = 750;
$date_array = array("2016-07", "2016-09");
$bill_paid_array =  array("2016-07" => [750,1], "2016-09" => [600,0]);

$is_full_amount = 1;
$previous_month = 0;
$x=0;

do {

$found = false;
while (!$found) {
    $date = date('2016'.'-'.'07'.'-d');
    $time = strtotime($date);
    $bill_month = date("Y-m", strtotime( ($x++)." month", $time));
    if (in_array($bill_month,$date_array)){
        if ($bill_paid_array["$bill_month"][1] == 0){
            $found = true;
            $previous_month = $monthly -$bill_paid_array["$bill_month"][0];
        }
    }else
        $found = !in_array($bill_month,$date_array) ;

}
if ($previous_month >0){
    echo date($bill_month."-d") . "=> $previous_month<br/>\n";
    $paid -= $previous_month;
    $previous_month = 0;
    $bill_paid_array["$bill_month"][1] = 1;
    $bill_paid_array["$bill_month"][0] =  $monthly;

}else {
    echo date($bill_month."-d") . "=> $monthly<br/>\n";
    $date_array[]=$bill_month;
    $bill_paid_array[$bill_month] = [$monthly,$is_full_amount];
    $paid -= $monthly;

}

if ($paid < $monthly) {
    $monthly = $paid;
    $is_full_amount = 0;
}
} while ($paid > 0);

希望它能按你的意愿运作:)

答案 1 :(得分:0)

试试这个

while (!$var) {
    $date = date('2016'.'-'.'07'.'-d');
    $time = strtotime($date);
    $bill_month = date("Y-m", strtotime( ($x++)." month", $time));
    if (in_array($bill_month,$date_array)){
        if ($bill_paid_array["$bill_month"][1] == 0){
            $found = true;
            $previous_month = $monthly -$bill_paid_array["$bill_month"][0];
        }
    }else
        $found = !in_array($bill_month,$date_array) ;

}

答案 2 :(得分:-1)

<?php
$paid = 2000;
$monthly = 750;
$date_array = array("2016-07", "2016-09");
$bill_paid_array =  array("2016-07" => [750,1], "2016-09" => [600,1]);

$is_full_amount = 1;
$previous_month = 0;
$x=0;

do {

$found = false;
while (!$found) {
    $date = date('2016'.'-'.'07'.'-d');
    $time = strtotime($date);
    $bill_month = date("Y-m", strtotime( ($x++)." month", $time));
    if (in_array($bill_month,$date_array)){
        if ($bill_paid_array["$bill_month"][1] == 0){
            $found = true;
            $previous_month = $monthly -$bill_paid_array["$bill_month"][0];
        }
    }else
        $found = !in_array($bill_month,$date_array) ;

}
if ($previous_month >0){
    echo date($bill_month."-d") . "=> $previous_month<br/>\n";
    $paid -= $previous_month;
    $previous_month = 0;
    $bill_paid_array["$bill_month"][1] = 1;
    $bill_paid_array["$bill_month"][0] =  $monthly;

}else {
    echo date($bill_month."-d") . "=> $monthly<br/>\n";
    $date_array[]=$bill_month;
    $bill_paid_array[$bill_month] = [$monthly,$is_full_amount];
    $paid -= $monthly;

}

} while ($paid > 0);