LoginSignup
2
0

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-10-30

ポイント

  • 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;
});
2
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
2
0