LoginSignup
0
0

More than 3 years have passed since last update.

【Laravel】Carbonで今が上期下期を判断する

Posted at

Laravelを用いてますが、Carboncomposerから入れれるので同じです。

上期は基本的に4月から9月末、下期は10月から3月末のようなので、そのように定義します。

今回はweb.phpにコールバックに書いていきます

web.php

Route::get('/',function(){
    $start = today()->month(4)->firstOfMonth();
    $end = today()->month(9)->endOfMonth();
});

上期の開始と終了を定義しました。
today()はLaravelに用意されているヘルパです。
new Carbon('today')と同等になります。

web.php

Route::get('/',function(){
    $start = today()->month(4)->firstOfMonth();
    $end = today()->month(9)->endOfMonth();
    echo today()->between($start , $end ) ? '上' : '下');
});

あとは、今日が上期の範囲内か判断しているだけです。

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