0
0

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.

[Laravel]Carbonで月初から月末まで何週あるか取得する

Last updated at Posted at 2020-09-24

週別の集計結果を表示する必要があったため忘れないようメモ。

サンプルコード

月初から月末まで何週あるかCarbonで取得。

$start = new Carbon()->firstOfMonth();
$end = new Carbon()->endOfMonth()

getWeekOfNumberInMonth($start, $end);
function getWeekOfNumberInMonth($start, $end) {
    $dt = $start->copy()->firstOfMonth();
    $week = 1;
    for($dt; $end >= $dt; $dt->addDay()) {
        if ($dt->isSunday() && $dt !== $start) $week++;
    }
    return $week;
}

FYI:
既出記事ですが取得ロジックを変えてやってます。
https://qiita.com/fabled/items/2607186731adc75f11e1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?