shibbir ahmed shibbir ahmed - 21 days ago 11
PHP Question

how to add data with date based on condition?

Following is my php code which return date with amount until the condition is false :

<?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);


Return Data :

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


How it's working :

This code is checking which monthly bill user paid already from starting date. If the month exist then next month is showing until condition is false.

Now for : e.g: starting date is :
2016-07-01
and user already paid
2016-07
and
2016-09
month bill (see $date_array)

Now user is paying
2000
and his bill amount is
750
. So that code is showing following date with amount.

Return Data :

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


What I want now

Now if I paid a new bill
750
again then according to above code this line :
date($bill_month."-d") . "=> $monthly<br/>\n";
will show me
2016-12-16
date: But which I don't want. I want to show :
2016-11-16
with
250
and
2016-12-16
with
500
.

Note : I have $bill_paid_array for each month

Update :

$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]";
}


Output :

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

Answer

check this one, I modified the $bill_paid_array, if you have any question I will explain

<?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);

Hope it will work as you want :)

Comments