预期的输入:

getDatesFromRange( '2010-10-01', '2010-10-05' );

预期的输出:

Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )

当前回答

  function GetDays($sStartDate, $sEndDate){  
      // Firstly, format the provided dates.  
      // This function works best with YYYY-MM-DD  
      // but other date formats will work thanks  
      // to strtotime().  
      $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));  
      $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));  

      // Start the variable off with the start date  
     $aDays[] = $sStartDate;  

     // Set a 'temp' variable, sCurrentDate, with  
     // the start date - before beginning the loop  
     $sCurrentDate = $sStartDate;  

     // While the current date is less than the end date  
     while($sCurrentDate < $sEndDate){  
       // Add a day to the current date  
       $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));  

       // Add this new day to the aDays array  
       $aDays[] = $sCurrentDate;  
     }  

     // Once the loop has finished, return the  
     // array of days.  
     return $aDays;  
   }  

使用像

GetDays('2007-01-01', '2007-01-31'); 

其他回答

使用DateTime对象的PHP 5.2解决方案。但是startDate必须在endDate之前。

function createRange($startDate, $endDate) {
    $tmpDate = new DateTime($startDate);
    $tmpEndDate = new DateTime($endDate);

    $outArray = array();
    do {
        $outArray[] = $tmpDate->format('Y-m-d');
    } while ($tmpDate->modify('+1 day') <= $tmpEndDate);

    return $outArray;
}

使用:

$dates = createRange('2010-10-01', '2010-10-05');

$日期包含:

Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )       

为了让穆斯塔法的回答更完整,这绝对是最简单和最有效的方法:

function getDatesFromRange($start_date, $end_date, $date_format = 'Y-m-d')
   {
      $dates_array = array();
      for ($x = strtotime($start_date); $x <= strtotime($end_date); $x += 86400) {
         array_push($dates_array, date($date_format, $x));
      }

      return $dates_array;
   }

   // see the dates in the array
   print_r( getDatesFromRange('2017-02-09', '2017-02-19') );

如果在调用函数时添加第三个参数,您甚至可以更改默认的输出日期格式,否则它将使用默认格式,即设置为'Y-m-d'。

我希望它能帮助你:)

简单却有魅力:

    $period = new DatePeriod(new DateTime('2015-01-01'), new DateInterval('P1D'), new DateTime('2015-01-15 +1 day'));
    foreach ($period as $date) {
        $dates[] = $date->format("Y-m-d");
    }

    //ONLY SHOWING
    echo '<pre>';
    var_dump($dates);
    echo '</pre>';
    exit();
function createDateRangeArray($start, $end) {
// Modified by JJ Geewax

$range = array();

if (is_string($start) === true) $start = strtotime($start);
if (is_string($end) === true ) $end = strtotime($end);

if ($start > $end) return createDateRangeArray($end, $start);

do {
$range[] = date('Y-m-d', $start);
$start = strtotime("+ 1 day", $start);
}
while($start < $end);

return $range;
} 

来源:http://boonedocks.net/mike/archives/137-Creating-a-Date-Range-Array-with-PHP.html

有很多方法可以做到这一点,但最后这都取决于您使用的PHP版本。以下是所有解决方案的总结:

获取PHP版本:

echo phpinfo();

PHP 5。+。

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

PHP 4 +

/**
 * creating between two date
 * @param string since
 * @param string until
 * @param string step
 * @param string date format
 * @return array
 * @author Ali OYGUR <alioygur@gmail.com>
 */
function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { 

    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);

    while( $current <= $last ) { 

        $dates[] = date($format, $current);
        $current = strtotime($step, $current);
    }

    return $dates;
}

PHP < 4

你应该升级:)