2017-01-03 166 views
2

我保存的客户必须根据他们的选择进行的付款次数,所以当付款日期到来时,我还可以更新“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 
) 

任何帮助将不胜感激。

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

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

你的代码是给完美的解决方案,因为如果你看到的输出(第一个),每次一个月改变: - 2017-01-02,2017-03-02 .... .......等等 –

+0

@安南,是和不是,在第一个输出上,第一个日期是正确的,但是下一个日期是不正确的,应该是,2017-02-02 ... – Tanker

+0

can你准确地把你想要的东西放到什么地方,我很迷茫,看最后两个 –

回答

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

+0

是的,它似乎在工作,我也用“期望”输出更新了我的问题,谢谢。 (我的日期很糟糕......) – Tanker

+0

@Tanker很乐意帮助你。 :) :) –

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>";