Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过使用碳元素传递特定月份名称,返回下个月名称_Php_Laravel_Php Carbon - Fatal编程技术网

Php 通过使用碳元素传递特定月份名称,返回下个月名称

Php 通过使用碳元素传递特定月份名称,返回下个月名称,php,laravel,php-carbon,Php,Laravel,Php Carbon,我需要通过向函数传递月份名称来获取下一个月和上一个月的名称 当我这样做时: $date = Carbon::now(); // suppose Current month is May $lastMonth = $date->subMonth()->format('F'); // returns April $nextMonth = $date->addMonth()->format('F'); // returns June 上面的代码运行良好。但我有一个函数,需

我需要通过向函数传递月份名称来获取下一个月和上一个月的名称

当我这样做时:

$date = Carbon::now(); // suppose Current month is May
$lastMonth =  $date->subMonth()->format('F'); // returns April
$nextMonth =  $date->addMonth()->format('F'); // returns June
上面的代码运行良好。但我有一个函数,需要传递月份名称:

 $month = "Feburary"; // it can be any random month
 function getNextMonth($month)
    {
       //$date = Carbon::now();
       return $date->addMonth()->format('F'); // need the output to be March
    }

在该函数中,如何使用
$month
名称获取下一个月的名称?

您可以使用
createFromFormat

Carbon::createFromFormat('F-d', "$month-1")->addMonth()->format('F');
-d
/
-1
只是为了确保它始终是月初,而不是根据当前日期溢出到下个月


(大约是从这个链接往下走的第十个街区)

a好+1对于解释,我使用
subMonthNoOverflow
addMonthNoOverflow
,我也需要
-d
/
-1
吗?@Saurabh我相信是这样的,因为这些方法用于修改实例的方法,其中
-d
/
-1
位用于创建实例。我的最终代码
Carbon::createFromFormat('F-d',“$month-1”)->addMonthNoOverflow()->格式('F')就像一个符咒。添加了
NoOverflow
,因为我在有31天(奇怪)的月份遇到了问题@Saurabh我相信是这样的,因为这些方法用于修改实例,其中
-d
/
-1
位用于创建实例。