ソースコード。


<?php

// ref. PHP: Date/Time - Manual
//      http://php.net/manual/ja/book.datetime.php

// 開始日を設定
$beginTime = '2016-04-30 00:00:00';
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, $beginTime);

// 今日の日付を取得
$today = new DateTime();
$today->setTime(0, 0, 0);

// 1日ずつ足していくためにDateIntervalオブジェクトを生成
$interval = new DateInterval('P1D'); // 1日間

// 日付出力用フォーマット
$dateFormat = 'Y-m-d';

// 今日の日付になるまでループ
while($date <= $today){

  // 日付を出力
  $output = $date->format($dateFormat);
  print("foo $output bar\n");

  // 1日ずつ足していく
  $date->add($interval);
}

?>

実行結果。


foo 2016-04-30 bar
foo 2016-05-01 bar
foo 2016-05-02 bar
foo 2016-05-03 bar
foo 2016-05-04 bar
foo 2016-05-05 bar
foo 2016-05-06 bar
foo 2016-05-07 bar
foo 2016-05-08 bar
foo 2016-05-09 bar
foo 2016-05-10 bar
foo 2016-05-11 bar
foo 2016-05-12 bar
foo 2016-05-13 bar
foo 2016-05-14 bar
foo 2016-05-15 bar
foo 2016-05-16 bar
foo 2016-05-17 bar
foo 2016-05-18 bar
foo 2016-05-19 bar
foo 2016-05-20 bar
foo 2016-05-21 bar

今回の環境: Debian jessie + PHP 5.6


$ uname -mrsv
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-1 (2016-03-06) x86_64

$ cat /etc/debian_version
8.4

$ php --version
PHP 5.6.20-0+deb8u1 (cli) (built: Apr 27 2016 11:26:05) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

tags: php

Posted by NI-Lab. (@nilab)