【问题标题】:Displaying of month and it's year based on condition根据条件显示月份和年份
【发布时间】:2016-11-05 07:14:06
【问题描述】:

在我的 Laravel 5.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
    ]
]

到目前为止我尝试过的代码是:

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

有人可以帮忙吗?提前致谢。

【问题讨论】:

  • 您想要将 monthName 属性与所有其他属性一起使用吗?或者作为一个只有monthNames的子数组,它驻留在具有其他属性的记录中?
  • @trincot 我需要 monthName 属性以及所有其他属性.. 但你知道吗,它们中的任何一个都对我有用..
  • @splash58 你不认为date("F, Y"); 会返回当前时间戳的年份和月份吗??
  • @SaiyanPrince 为什么不呢? - eval.in/599829我好像没看懂问题

标签: php arrays date laravel-5.1 php-carbon


【解决方案1】:

您尝试使用的代码将始终使用当前年份,因此如果订阅期在另一年,它将不起作用。

您可以在您的代码中使用这种变体,它利用了一些 Carbon 和 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');
}

【讨论】:

    【解决方案2】:

    你可以对月数做一个循环,使用Carbon对象的add方法增加1个月。然后,格式化只是调用 format 方法的问题,正如 trincot 已经建议的那样

    【讨论】:

      猜你喜欢
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2011-06-11
      • 1970-01-01
      相关资源
      最近更新 更多