付款日期基于时间间隔

I was saving the number of payments that a client has to do based on their selection, so when the date of payment came in I can also update "payments_left" and "next_payment_date", but they want me to save all payment dates on a separate table.

Something like, id, id_order, amount, payment_date, status ... and now that I think about it, it make senses, the problem is that I have no clue how to get the "next_payment_date".

$today = date('Y-m-d', strtotime('2017-01-02 20:27:49'));

$howmany_payments = 2; // How many payments, 
                 // 1 = year
                 // 2 = 1 every 6 months until 1 year from $today
                 // 4 = 1 every 3 months until 1 year from $today
                 // full payment has to be done in 1 year.

$months = 12 / $howmany_payments;

$ar_dates = array();
$ar_dates[] = date('Y-m-d', strtotime($today)); // Set first payment
$cct = $months + 1;
$tnps = $howmany_payments - 1;
for($i = 1; $i <= $tnps; $i++){
    $ar_dates[] = date('Y-m-d', strtotime("+$cct months", strtotime($today)));
    $cct+=$months + 1;
}


print_r($ar_dates);

What I need is every payment date starting from the date that the order is placed... in this case 2017-01-02 20:27:49...

My code is some what working but is not that accurate.

//    if payments are every month, this is the array I get
    Array
    (
        [0] => 2017-01-02
        [1] => 2017-03-02
        [2] => 2017-05-02
        [3] => 2017-07-02
        [4] => 2017-09-02
        [5] => 2017-11-02
        [6] => 2018-01-02
        [7] => 2018-03-02
        [8] => 2018-05-02
        [9] => 2018-07-02
        [10] => 2018-09-02
        [11] => 2018-11-02
    )
// if I want to make 4 payments this is the array
Array
(
    [0] => 2017-01-02
    [1] => 2017-05-02
    [2] => 2017-09-02
    [3] => 2018-01-02
)
// which I think is ok... 
// if I want to make 2 payments the array is this:
Array
(
    [0] => 2017-01-02
    [1] => 2017-08-02
)

Any help would be greatly appreciated.

****UPDATE**** The output I'm looking for is something like:

// if Payment is every month
Array
(
    [0] => 2017-01-02
    [1] => 2017-02-02
    [2] => 2017-03-02
    [3] => 2017-04-02
    [4] => 2017-05-02
    [5] => 2017-16-02
    [6] => 2017-07-02
    [7] => 2017-08-02
    [8] => 2017-09-02
    [9] => 2017-10-02
    [10] => 2017-11-02
    [11] => 2017-12-02
)
// if Payment is every 3 months
Array
(
    [0] => 2017-01-02
    [1] => 2017-05-02
    [2] => 2017-09-02
    [3] => 2018-01-02
)
// if Payment is every 6 months

Array
(
    [0] => 2017-01-02
    [1] => 2017-07-02
)
// ... and so on...

If i am correctly getting you then you want something like this:-

<?php
$today = date('Y-m-d', strtotime('2017-01-03 10:00:00'));

$howmany_payments = 12; // How many payments, 
             // 1 = year
             // 2 = 1 every 6 months until 1 year from $today
             // 3 = 1 every 4 months until 1 year from $today
             // 4 = 1 every 3 months until 1 year from $today
             // 6 = 1 every 2 months until 1 year from $today
             // 12 = 1 every 1 months until 1 year from $today

$months = 12 / $howmany_payments;

$ar_dates = array();
$ar_dates[] = date('Y-m-d', strtotime($today)); // Set first payment
$tnps = $howmany_payments - 1;
for($i = 1; $i <= $tnps; $i++){
    $new_date  = date('Y-m-d', strtotime("+$months months", strtotime($today))); // assign the next date to new_date variable
    $ar_dates[] = $new_date; // insert the next date into array
    $today = $new_date; // assign new date to today
}


echo "<pre/>";print_r($ar_dates);

Output:- https://eval.in/707936

You are adding 1 which is creating problem. You should change to $cct = $months; and $cct +=$months;

<?php 
$today = date('Y-m-d', strtotime('2017-01-02 20:27:49'));

$howmany_payments = 4; // How many payments, 
                 // 1 = year
                 // 2 = 1 every 6 months until 1 year from $today
                 // 4 = 1 every 3 months until 1 year from $today
                 // full payment has to be done in 1 year.

$months = 12 / $howmany_payments;

$ar_dates = array();
$ar_dates[] = date('Y-m-d', strtotime($today)); // Set first payment
$cct = $months;
$tnps = $howmany_payments - 1;
for($i = 1; $i <= $tnps; $i++){
    $ar_dates[] = date('Y-m-d', strtotime("+$cct months", strtotime($today)));
    $cct +=$months;
}
echo "<pre>";
print_r($ar_dates);
echo "</pre>";