1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[php]毎月の月初日の配列を、作成したい

Last updated at Posted at 2021-07-14

DatePriodが優秀なので、こんな感じで簡単に取得できます。
日毎の配列も作成できますし、範囲の指定方法もDatetimeを操作するだけなので、便利ですね!

qiita.php
$datePeriod = (new DatePeriod(
    new DateTime('2021/01/01'),
    new DateInterval('P1M'),
    new DateTime('2022/01/01'),
));

foreach ($datePeriod as $dt) {
     echo $dt->format('Y/m')."\n";
}
//出力結果
//2021/01
//2021/02
//2021/03
//2021/04
//2021/05
//2021/06
//2021/07
//2021/08
//2021/09
//2021/10
//2021/11
//2021/12

参考

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?