34
18

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.

Carbon の lastOfMonth() と endOfMonth() の違い

Posted at

会社のコードの中で、lastOfMonth() と endOfMonth() が混在していた。何が違うのか調べてもまとまっている記事がなかったので書く。
調査にはLaravelのTinkerを使った。

環境

Laravel 6.2.0
Carbon 2系

前提

$ php artisan tinker
>>>
>>> echo today()
2019-10-17 00:00:00

lastOfMonth() と endOfMonth()

  • lastOfMonth() : その月の最終日が始まった瞬間が取得できる(00:00:00)
  • endOfMonth() : その月の最終日が終わる瞬間が取得できる(23:59:59)

-> 取得できる時間が違う


>>> echo today()->lastOfMonth();
2019-10-31 00:00:00
>>> echo today()->endOfMonth();
2019-10-31 23:59:59

firstOfMonth() と startOfMonth()

  • firstOfMonth() : その月の初日が始まった瞬間が取得できる(00:00:00)
  • startOfMonth() : その月の初日が始まった瞬間が取得できる(00:00:00)

-> 同じ!


>>> echo today()->firstOfMonth();
2019-10-01 00:00:00
>>> echo today()->startOfMonth();
2019-10-01 00:00:00

lastOfYear() と endOfYear()

  • lastOfYear() : その年の最終日が始まった瞬間が取得できる(00:00:00)
  • endOfYear() : その年の最終日が終わる瞬間が取得できる(23:59:59)

-> 取得できる時間が違う


>>> echo today()->lastOfYear();
2019-12-31 00:00:00
>>> echo today()->endOfYear();
2019-12-31 23:59:59

firstOfYear() と startOfYear()

  • firstOfYear() : その年の初日が始まった瞬間が取得できる(00:00:00)
  • startOfYear() : その年の初日が始まった瞬間が取得できる(00:00:00)

-> 同じ!


>>> echo today()->firstOfYear();
2019-01-01 00:00:00
>>> echo today()->startOfYear();
2019-01-01 00:00:00

lastOfWeek() と endOfWeek()

  • lastOfWeek() : 存在しない
  • endOfWeek() : その週の最終日が終わる瞬間が取得できる(23:59:59)

>>> echo today()->endOfWeek();
2019-10-20 23:59:59

firstOfWeek() と startOfWeek()

  • firstOfWeek() : 存在しない
  • startOfWeek() : その週の初日が始まった瞬間が取得できる(00:00:00)

>>> echo today()->startOfWeek();
2019-10-14 00:00:00

lastOfDay() と endOfDay()

  • lastOfDay() : 存在しない
  • endOfDay() : その日が終わる瞬間が取得できる(23:59:59)

>>> echo today()->endOfDay();
2019-10-17 23:59:59

firstOfDay() と startOfDay()

  • firstOfDay() : 存在しない
  • startOfDay() : その日が始まった瞬間が取得できる(00:00:00)

>>> echo today()->startOfDay();
2019-10-17 00:00:00
34
18
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
34
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?