Help us understand the problem. What is going on with this article?

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

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 ) ? '上' : '下');
});

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした