LoginSignup
5
3

More than 3 years have passed since last update.

PHPで年度を取得する~DateTimeとCarbon~

Last updated at Posted at 2020-01-27

やりたいこと

DateTimeクラスとCarbonライブラリそれぞれ、任意の日付から年度を取得します。

一般的な年度(4月1日~翌年3月31日までを年度)とします。
※2019年を例にすると、2019年4月1日から2020年3月31日までのことを指します。

DateTimeクラス

$date = new DateTime('2020-03-1');
dump($date->modify('-3 month')->format('Y')); // 2019

Carbonライブラリ

use Carbon\CarbonImmutable;

$carbon = new CarbonImmutable('2020-03-1');
dump($carbon->subMonthsNoOverflow(3)->format('Y')); // 2019

間違ってる記載がありましたら教えていただけると嬉しいですm(__)m。

参考文献

この記事は以下の情報を参考にしました。

5
3
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
5
3