付款日期基于间隔

时间:2017-01-03 05:36:59

标签: php

我根据他们的选择保存了客户必须支付的付款次数,因此当付款日期到来时我还可以更新“payments_left”和“next_payment_date”,但他们希望我保存所有付款日期在另一张桌子上。

像id,id_order,amount,payment_date,status这样的东西......现在我想起来,它会让人感觉到,问题在于我不知道如何获得“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);

我需要的是从订单发出之日起的每个付款日期......在这种情况下2017-01-02 20:27:49 ...

我的代码是有效的,但不准确。

//    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
)

非常感谢任何帮助。

**** **** UPDATE 我正在寻找的输出是这样的:

// 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...

2 个答案:

答案 0 :(得分:2)

如果我正确地找到你,那么你想要这样的东西: -

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

输出: - https://eval.in/707936

答案 1 :(得分:1)

您正在添加1,这会产生问题。您应该更改为$cct = $months;$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>";