从年初开始获取前几个月的列表

发布于 2025-01-24 23:03:49 字数 463 浏览 0 评论 0原文

我正在尝试使用获取前几个月的列表。

预期结果:

January, February, March, April

到目前为止所做的事情:

$now = Carbon::now();
$startMonth = $now->startOfMonth()->subMonth($now->month);
$currentMonth = $now->startOfMonth();
$diff = $currentMonth->diffInMonths($startMonth);

我使用Carbon :: Now()()获得了一年中的第一个月,然后我尝试计算日期之间的差异,但我得到了0

同样,我也会找到任何将月份列表作为预期产出的列表的方法。

I'm trying to get a list of previous months using Carbon.

Expected result:

January, February, March, April

What I did so far:

$now = Carbon::now();
$startMonth = $now->startOfMonth()->subMonth($now->month);
$currentMonth = $now->startOfMonth();
$diff = $currentMonth->diffInMonths($startMonth);

I've used Carbon::now() to get the first month of the year, then I tried to calculate the difference between the dates but I got 0.

Also I canno find any method that returns a list of months as the expected output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悸初 2025-01-31 23:03:49

为什么不一个简单的循环?

use Carbon\Carbon;

$previousMonths = [];

$currentDate = Carbon::now()->startOfMonth();
while ($currentDate->year == Carbon::now()->year) {
  $previousMonths[] = $currentDate->format('F');
  $currentDate->subMonth();
} 

$ preathths现在是:

[
  "April",
  "March",
  "February",
  "January",
]

编辑


如果需要相反的顺序,则

$previousMonths = array_reverse($previousMonths);

,然后:然后$ preathsmonths将是:

[
  "January",
  "February",
  "March",
  "April",
]

Why not a simple while loop?

use Carbon\Carbon;

$previousMonths = [];

$currentDate = Carbon::now()->startOfMonth();
while ($currentDate->year == Carbon::now()->year) {
  $previousMonths[] = $currentDate->format('F');
  $currentDate->subMonth();
} 

$previousMonths would now be:

[
  "April",
  "March",
  "February",
  "January",
]

Edit


If you need them in the opposite order, then:

$previousMonths = array_reverse($previousMonths);

Then $previousMonths will be:

[
  "January",
  "February",
  "March",
  "April",
]
风追烟花雨 2025-01-31 23:03:49

您将原始变量设置为上一年的12月1日,因为您不断修改原始变量而不是复制它。相反,创建两个变量,从now()中创建两个变量,然后您可以使用它来创建Carbonperiod以迭代。

$firstOfYear = Carbon::now()->firstOfYear();
$firstOfLastMonth = Carbon::now()->firstOfMonth()->subMonth(); // If you want to include the current month, drop ->subMonth()
$period = CarbonPeriod::create($firstOfYear, '1 month', $firstOfLastMonth);
foreach($period as $p) echo $p->format('F')."\n";

You're setting the original variable to December 1st of the previous year, since you keep modifying the original instead of copying it. Instead, create two variables off of now(), then you can use that to create a CarbonPeriod to iterate through.

$firstOfYear = Carbon::now()->firstOfYear();
$firstOfLastMonth = Carbon::now()->firstOfMonth()->subMonth(); // If you want to include the current month, drop ->subMonth()
$period = CarbonPeriod::create($firstOfYear, '1 month', $firstOfLastMonth);
foreach($period as $p) echo $p->format('F')."\n";
北陌 2025-01-31 23:03:49

使用PHP范围的另一种方法:

$now = Carbon::now();
$months = collect( range(1, $now->month) )->map( function($month) use ($now) {
    return Carbon::createFromDate($now->year, $month)->format('F');
})->toArray();

从一月到本月(类似于预期的结果),这应该返回几个月的收集(如果使用Toarray()的数组)

Another approach using PHP range:

$now = Carbon::now();
$months = collect( range(1, $now->month) )->map( function($month) use ($now) {
    return Carbon::createFromDate($now->year, $month)->format('F');
})->toArray();

This should return a collection of months (array if toArray() is used) from January until the current month (similar to the expected result)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文