2017-01-03 4 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 
) 

ご協力いただければ幸いです。

****の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... 
+0

に変更する必要があります - 2017-01-02,2017-03-02 .... .......など –

+0

@Anant、yesとno、最初の出力は最初の日付は正しいが、次の日付は正しくない、2017-02-02 ... – Tanker

+0

canあなたが正確に何を来てほしいと思うか、私は最後の2つを見て混乱しています –

答えて

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

@タンカーはあなたを助けてうれしいです。 :) :) –

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>"; 
関連する問題