预期的输入:
getDatesFromRange( '2010-10-01', '2010-10-05' );
预期的输出:
Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )
预期的输入:
getDatesFromRange( '2010-10-01', '2010-10-05' );
预期的输出:
Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )
当前回答
你必须添加$end->modify('+1 day')来包含间隔的最后一天,例如,如果不使用modify()方法,一月份将有31天而不是30天。 这个版本的代码将包括间隔的最后一天:
$begin = new DateTime( '2018-08-01' );
$end = new DateTime( '2018-08-31' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
echo $date->format("Ymd") . "<br>";
}
PHP文档链接
其他回答
这很简短,而且应该在PHP4+中工作。
function getDatesFromRange($start, $end){
$dates = array($start);
while(end($dates) < $end){
$dates[] = date('Y-m-d', strtotime(end($dates).' +1 day'));
}
return $dates;
}
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');
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
这是非常灵活的。
/**
* Creating date collection between two dates
*
* <code>
* <?php
* # Example 1
* date_range("2014-01-01", "2014-01-20", "+1 day", "m/d/Y");
*
* # Example 2. you can use even time
* date_range("01:00:00", "23:00:00", "+1 hour", "H:i:s");
* </code>
*
* @author Ali OYGUR <alioygur@gmail.com>
* @param string since any date, time or datetime format
* @param string until any date, time or datetime format
* @param string step
* @param string date of output format
* @return array
*/
function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
我认为这是最简短的答案
按照您的喜好编辑代码
for ($x=strtotime('2015-12-01');$x<=strtotime('2015-12-30');$x+=86400)
echo date('Y-m-d',$x);