显示月份及其年份

时间:2016-07-02 12:51:55

标签: php arrays laravel-5.1 php-carbon

我有一个像这样的数组:

1 => array:7 [▼
  "id" => "4"
  "order_code" => "BS-ORD-000000004"
  "order_type" => "Mixed - Subscription"
  "subscription_months" => "4"
  "subscription_start_month" => Carbon {#714 ▼
    +"date": "2016-11-05 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "subscription_end_month" => Carbon {#723 ▼
    +"date": "2017-03-04 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "monthName" => "November"
]
2 => array:7 [▼
  "id" => "4"
  "order_code" => "BS-ORD-000000004"
  "order_type" => "Mixed - Subscription"
  "subscription_months" => "4"
  "subscription_start_month" => Carbon {#721 ▼
    +"date": "2016-11-05 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "subscription_end_month" => Carbon {#717 ▼
    +"date": "2017-03-04 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "monthName" => "December"
]
3 => array:7 [▼
  "id" => "4"
  "order_code" => "BS-ORD-000000004"
  "order_type" => "Mixed - Subscription"
  "subscription_months" => "4"
  "subscription_start_month" => Carbon {#729 ▼
    +"date": "2016-11-05 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "subscription_end_month" => Carbon {#730 ▼
    +"date": "2017-03-04 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "monthName" => "January"
]
4 => array:7 [▼
  "id" => "4"
  "order_code" => "BS-ORD-000000004"
  "order_type" => "Mixed - Subscription"
  "subscription_months" => "4"
  "subscription_start_month" => Carbon {#732 ▼
    +"date": "2016-11-05 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "subscription_end_month" => Carbon {#733 ▼
    +"date": "2017-03-04 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "monthName" => "February"
]

现在我想要的是在从monthName键添加月份后,在subscription_start_month键附加一年。

我想要的例子:

2 => array:7 [▼
  "id" => "4"
  "order_code" => "BS-ORD-000000004"
  "order_type" => "Mixed - Subscription"
  "subscription_months" => "4"
  "subscription_start_month" => Carbon {#721 ▼
    +"date": "2016-11-05 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "subscription_end_month" => Carbon {#717 ▼
    +"date": "2017-03-04 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "monthName" => "December, 2016" // <-- The current year
]
3 => array:7 [▼
  "id" => "4"
  "order_code" => "BS-ORD-000000004"
  "order_type" => "Mixed - Subscription"
  "subscription_months" => "4"
  "subscription_start_month" => Carbon {#729 ▼
    +"date": "2016-11-05 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "subscription_end_month" => Carbon {#730 ▼
    +"date": "2017-03-04 00:00:00.000000"
    +"timezone_type": 3
    +"timezone": "UTC"
  }
  "monthName" => "January, 2017" // <-- The next year
]

我尝试了以下代码,但它没有按预期工作。

$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:

场景:用户在2016年10月订阅了6个月..所以,阵列看起来像这样:

[
    "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"
]

我展示的上述数组只有1个月..数组中还有5个元素..

我想要的是,我想在Year之后附加monthName ..

所以数组应如下所示:

[
    [
        "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
    ]
]

我怎样才能实现这一目标?请帮帮我..

1 个答案:

答案 0 :(得分:0)

鉴于你有Carbon对象,你可以使用year属性:

foreach($data as $i => $row) {
    $data[$i]["monthName"] .= ", " . $row["subscription_start_month"]->year;
}

或者,您可以使用Carbon的format

从头开始格式化日期
foreach($data as $i => $row) {
    $data[$i]["monthName"] = $row["subscription_start_month"]->format('F, Y');
}