2016-07-03 7 views
0

私のLaravel 5.1アプリケーションでは、Subscriptionの機能があります。ユーザーが.. 6ヶ月間2016年10月のサブスクリプションを作るので、配列は次のようになります:月の表示と条件に基づく年

私が..

シナリオを探しています何ある程度作業ではなく、私が示しています上記の配列は、わずか1ヶ月である

[ 
    "subscription_months" => 6 
    "subscription_start_month" => "01-10-2016" // <-- will be carbon instance 
    "subscription_end_month => "31-03-2017" // <-- will be carbon instance 
    "monthName" => "October" 
] 

..配列の5つの以上の要素があるでしょう

私は追加したい、で欲しいYearmonthName

これはこういう風に見えます。

[ 
    [ 
     "monthName" => "October, 2016" // <-- for 1st month 
    ] 

    [ 
     "monthName" => "November, 2016" // <-- for 2nd month 
    ] 

    [ 
     "monthName" => "December, 2016" // <-- for 3rd month 
    ] 

    [ 
     "monthName" => "January, 2017" // <-- for 4th month 
    ] 

    [ 
     "monthName" => "February, 2017" // <-- for 5th month 
    ] 

    [ 
     "monthName" => "March, 2017" // <-- for 6th month 
    ] 
] 

私がこれまで試してみましたコードは次のとおりです。

$startMonthForMixed = $invoice->subscription_start_date->month; 

for($i = 0; $i < $invoice->subscription_months; $i++) { 
    $convertedInvoices[] = [ 
     'id' => $invoice->id, 
     'order_code'    => $invoice->order_code, 
     'order_type'    => 'Mixed - Subscription', 
     'subscription_months'  => $invoice->subscription_months, 
     'subscription_start_month' => $invoice->subscription_start_date, 
     'subscription_end_month' => $invoice->subscription_end_date, 
     'monthName'    => date("F", mktime(0, 0, 0, $startMonthForMixed, 01)) . ", " . Carbon::now()->addYear() 
    ]; 
    $startMonthForMixed++; 
} 

誰がこれを助けることはできますか?前もって感謝します。

+1

'date(" F、Y "'? – splash58

+0

他のすべてのプロパティと共にmonthNameプロパティを必要とするか、 – trincot

+0

@trincot他のすべてのプロパティと一緒にmonthNameプロパティが必要です..しかし、それらのいずれかが私のために働くでしょう。 –

答えて

0

あなたが試したコードは常に現在の年を使用するため、サブスクリプション期間が別の年の場合は機能しません。あなたは、いくつかの炭素とのDateTimeメソッドを使用していますあなたのコード、上のこの変化を使用することができ

// take copy of start date 
$subscription_month = $invoice->subscription_start_date->copy(); 

for($i = 0; $i < $invoice->subscription_months; $i++) { 
    $convertedInvoices[] = [ 
     'id' => $invoice->id, 
     'order_code'    => $invoice->order_code, 
     'order_type'    => 'Mixed - Subscription', 
     'subscription_months'  => $invoice->subscription_months, 
     'subscription_start_month' => $invoice->subscription_start_date, 
     'subscription_end_month' => $invoice->subscription_end_date, 
     'monthName'    => $subscription_month->format("F, Y") 
    ]; 
    // move to first date of following month 
    $subscription_month->modify('first day of next month'); 
} 
0

あなたは追加の炭素オブジェクトのadd方法を使用して、数ヶ月のサイクルを行うことができます1ヶ月。次に、書式設定は、すでに提案されているように、formatメソッドを呼び出すだけです。

関連する問題