1
0

More than 3 years have passed since last update.

Carbon subMonthではなくsubMonthsNoOverflowを使用する

Last updated at Posted at 2021-03-30

Carbonでの月の加算減算でsubMonthを使用していると下記のようなことが起こってしまうので、subMonthsNoOverflowを使用したほうが良いです。

subMonthを使用した場合

$date = new Carbon('2021-03-29');
$last_month = $date->subMonth();
// 2021-03-01

subMonthsNoOverflowを使用した場合

$date = new Carbon('2021-03-29');
$last_month = $date->subMonthsNoOverflow();
// 2021-02-28
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0