dongpiansui8755 2016-07-02 12:51
浏览 17

显示月份及其年份

I have an array like so:

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

Now what I want is to append a year to the monthName key after adding the months from subscription_start_month key.

Example of what I want:

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
]

I have the tried the following code, but it does not work as expected.

$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++;
 }

UPDATE 1:

Scenario: A user makes a subscription on October 2016 for 6 months.. So, the array will look something like this:

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

The above array I am showing is only 1 month.. There will be 5 more elements in the array..

What I want is, I want to append the Year after monthName..

So the array should look like this:

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

How can I achieve that ? Kindly help me out..

  • 写回答

1条回答 默认 最新

  • dongyu5482 2016-07-02 13:53
    关注

    Given that you have Carbon objects in there, you could use the year property:

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

    Alternatively, you could just format the date from scratch, with Carbon's format:

    foreach($data as $i => $row) {
        $data[$i]["monthName"] = $row["subscription_start_month"]->format('F, Y');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法