1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CarbonPeriodで今日から月末までの日付の配列を作ろうとしたら日付が足りない

Last updated at Posted at 2022-02-01

Laravelで開発中、CarbonPeriodで今日から今月の月末までの日付の配列を作ろうとした。

2022年2月1日 ~ 2022年2月28日

$from_day = Carbon::now();
$to_day = Carbon::create($from_day)->lastOfMonth();

dd($from_day, $to_day,CarbonPeriod::create($from_day, $to_day)->toArray());

image.png
28日分だから28個の配列が出来ると思っていた。27日までしかあらへん。
image.png
時間が中途半端だから何かおかしいのか。
0時0分にしよう

$from_day = Carbon::now()->startOfDay();
$to_day = Carbon::create($from_day)->lastOfMonth();

dd($from_day, $to_day,CarbonPeriod::create($from_day, $to_day)->toArray());

image.png

ちゃんと28日分出来た。

日付の指定配列作る際は時間に気を付けましょう

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?