Carbon - 「本日を含む一ヶ月以内」であるかどうかチェック


ポイント

  • staftOfDay() で時刻をリセットする
  • Carbonは Carbon::now() を上書きする
    • copy() を忘れずに
  • between は第3引数で = ありの > <かどうかを指定
    • trueのとき = あり
Validator::extend("WithinAMonth", function ($attribute, $value, $parameters, $validator) {
    if ($value && \DateTime::createFromFormat("Y/m/d", $value)) {
        $date = Carbon::createFromFormat("Y/m/d", $value)->startOfDay();
        $today = Carbon::now()->startOfDay();
        $nextMonthDay = $today->copy()->addMonth();
        if ($date->between($today, $nextMonthDay, true)) {
            return true;
        }
        return false;
    }
    return false;
});