LoginSignup
8
2

More than 3 years have passed since last update.

PHPで週を扱う場合の注意点(ISO8601)

Last updated at Posted at 2019-12-03

PHPに限った話ではないと思いますが、、、
他社実装のカレンダーにバグがあって、調査していて知ったので備忘録です。

2018年12月最終週のカレンダーは以下の通り

30 31 1 2 3 4 5

これを踏まえ、2018年の12月30日(日)、12月31日(月)がそれぞれ年の何周目かを表示します。
まずは12月30日(日)から。

echo (new \DateTime('2018-12-30 00:00:00'))->format('W');

これは52が返ってきます。2018/12/30は年の52週目なんですね。
次に、2018/12/31について表示します。52を期待しましょう。

echo (new \DateTime('2018-12-31 00:00:00'))->format('W');

「01」
何ということでしょう。
2018/12/31は年の1週目ということです。

これは、ISO8601に基づいているのでこのようになります。
同様に、2012/1/1は1周目ではなく52週目になります。

プログラムで週を扱うときは注意が必要ですね。

【参照】
https://ja.wikipedia.org/wiki/ISO_8601#%E5%B9%B4%E3%81%A8%E9%80%B1%E3%81%A8%E6%9B%9C%E6%97%A5

8
2
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
8
2