Carbon 返回错误的日期? Laravel 5.6

Carbon is returning a wrong date? Laravel 5.6

我在 laravel 中遇到了 Carbon 的一个非常奇怪的问题...我正在尝试获取这样的特定日期:

    $now = Carbon::now()->setTimezone('America/Costa_Rica');

    $currentYear = $now->copy()->year;

    $febmon = $now->copy()->month(2)->startOfMonth();
    dd($febmon);

应该return:2018-02-01 00:00:00.0 America/Costa_Rica (-06:00)

但我得到的是:2018-03-01 00:00:00.0 America/Costa_Rica (-06:00)

我已经尝试过使用所有其他月份的数字,并且效果很好,但是二月...不知道哪里出了问题。提前致谢

好的,我发现了问题,我的错误,但如果有人面临这个简单但奇怪的问题:

我根据 now() 设置日期,并且在设置 startOfMonth()

之前设置 month(2)

并且因为今天是 30,所以它会传递到二月的下个月,也就是三月,因为二月没有 30 天,我所要做的就是先设置 startOfMonth()...所以它将采取正确的日期。

正确的方法是:

$now = Carbon::now()->setTimezone('America/Costa_Rica');

$febmon = $now->copy()->startOfMonth()->month(2); //Specify the month at last, and set the startOfMonth() first.
dd($febmon);