【发布时间】:2016-11-04 17:57:44
【问题描述】:
我有一个这样的数组:
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"
]
现在我想要的是在添加来自 subscription_start_month 键的月份之后,在 monthName 键上附加一年。
我想要的示例:
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 个元素..
我想要的是,我想在monthName 之后附加Year..
所以数组应该是这样的:
[
[
"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
]
]
我怎样才能做到这一点?请帮帮我..
【问题讨论】:
-
不确定要添加哪一年。开始日期的年份还是结束日期的年份?
-
@MassimilianoArione 根据年份,将
subscription_months添加到subscription_end_month.. -
@MassimilianoArione 我已经添加了 UPDATE 1 部分,请看一下..
标签: php arrays laravel-5.1 php-carbon