LoginSignup
8
13

More than 5 years have passed since last update.

【PHP】日付を一日ずつ進めて表示させてゆく

Last updated at Posted at 2017-11-27

日付を一日ずつ進めながら表示させたい。
結局、いつもこの方法を利用しているような気がする(もっと良い方法があれば教えてほしい)。

<?php

$start = '2017-11-15'; # 開始日時
$end = '2017-11-30'; # 終了日時

// dateを使う
for ($i = $start; $i <= $end; $i = date('Y-m-d', strtotime($i . '+1 day'))) {
    echo $i . PHP_EOL;
}

// DateTimeクラスを使う
/*
for ($i = new DateTime($start); $i <= new DateTimeImmutable($end); $i->modify('+1 day')) {
    echo $i->format('Y-m-d') . PHP_EOL;
}
*/
実行結果
2017-11-15
2017-11-16
2017-11-17
2017-11-18
2017-11-19
2017-11-20
2017-11-21
2017-11-22
2017-11-23
2017-11-24
2017-11-25
2017-11-26
2017-11-27
2017-11-28
2017-11-29
2017-11-30
8
13
3

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
13