Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

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