【问题标题】:How pass month value and get start and end date of month In laravel如何在laravel中传递月份值并获取月份的开始和结束日期
【发布时间】:2019-04-25 18:10:07
【问题描述】:

我是一个新的 Laravel 并使用 Laravel 5.5。

我想传递月份的值并获取过去月份的开始和结束日期。

假设通过5(5月)获取值 开始日期:01-05-2019 结束日期:30-05-2019

我试过了:

$finalMonth = 5;
$startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
$endMonth = Carbon::now()->addMonth($finalMonth)->endOfMonth()->format("Y-m-d");

但它给出了错误的结果,因为它添加了当前月份+过去月份。 表示 4+5=9 表示 9 月。

请帮忙

【问题讨论】:

    标签: laravel laravel-5.5


    【解决方案1】:

    而不是使用:

    $startMonth = Carbon::now()->addMonth($finalMonth)->day(1)->format("Y-m-d");
    

    改用month 方法:

    $startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");
    

    见:carbon-get-month-by-number-returns-march-for-number-2

    【讨论】:

      【解决方案2】:

      在下面使用:

      $startMonth = Carbon::now()->month($finalMonth)->day(1)->format("Y-m-d");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2014-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多