【问题标题】:I face some problem with getting last day of next month in PHP or PHP framework Laravel我在 PHP 或 PHP 框架 Laravel 中获取下个月的最后一天时遇到了一些问题
【发布时间】:2023-01-22 02:54:07
【问题描述】:

当我尝试通过 PHP 或 Laravel carbon 获取下个月的最后一天时,一切正常,但是当我尝试从特定日期 2023-01-31 获取下个月的最后一天时

date('Y-m-01', strtotime('next month', strtotime('2023-01-31')));

或者

Carbon::createFromFormat('Y-m-d H:m:00:000', '2023-01-31')->addMonth()->format('Y-m-t H:m:00:000');

那个时间输出结果在 2023-03-31 给出,但下个月的最后一天是 2023-02-28。

我该如何解决。请帮我。

$instituter = Institute::where('code', $instInfo->institute_code)->first();
            // $institute = Carbon::createFromFormat('Y-m-d H:m:00:000', $institute->institute_expire)->addMonth()->format('Y-m-d H:m:00:000');
            // $expire = $instituter->institute_expire;
            // $inst = Carbon::createFromFormat('Y-m-d H:i:00.000', $instituter->institute_expire)->addMonths(6)->startOfMonth()->format('Y-m-d');
            $inst = date('Y-m-01', strtotime('next month', strtotime($instituter->institute_expire)));
            // $inst = Carbon::createFromFormat('Y-m-d H:i:00.000', $instituter->institute_expire)->addMonths(6)->startOfMonth();
            // $institute = Carbon::createFromFormat('m/d/Y', $instituter->institute_expire)->addMonth();

它不能正常工作。

【问题讨论】:

    标签: php laravel datetime


    【解决方案1】:

    您可以使用 Carbon 中的 endOfMonth() 方法来获取下个月的最后一天,如下所示。

    $date = Carbon::createFromFormat('Y-m-d', '2023-01-31');
    $next_month_last_day = $date->addMonth()->endOfMonth()->format('Y-m-d');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多