我正面临着一个奇怪的问题我想找个这样的约会对象:
$now = Carbon::now()->setTimezone('America/Costa_Rica');
$currentYear = $now->copy()->year;
$febmon = $now->copy()->month(2)->startOfMonth();
dd($febmon);它应该返回:2018-02-01 00:00:00.0 America/Costa_Rica (-06:00)
但我得到的却是:2018-03-01 00:00:00.0 America/Costa_Rica (-06:00)
我已经试过所有其他月份的号码了,而且效果很好,但是二月.不知道是怎么回事。提前感谢
发布于 2018-05-30 07:21:13
好吧,我发现了这个问题,我的错误,但是如果有人面临这个简单而又奇怪的问题:
我根据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);https://stackoverflow.com/questions/50597837
复制相似问题