PHP:返回数组中两个日期之间的所有日期
预期输入:PHP:返回数组中两个日期之间的所有日期,php,datetime,date-range,Php,Datetime,Date Range,预期输入: getDatesFromRange( '2010-10-01', '2010-10-05' ); Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' ) echo phpinfo(); 预期输出: getDatesFromRange( '2010-10-01', '2010-10-05' ); Array( '2010-10-01', '2010-10-02', '2010-1
getDatesFromRange( '2010-10-01', '2010-10-05' );
Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )
echo phpinfo();
预期输出:
getDatesFromRange( '2010-10-01', '2010-10-05' );
Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )
echo phpinfo();
函数createDateRangeArray($start,$end){
//JJ蜡改性
$range=array();
如果(is_string($start)==true)$start=strottime($start);
如果(is_string($end)==true)$end=strottime($end);
如果($start>$end)返回createDateRangeArray($end,$start);
做{
$range[]=日期('Y-m-d',$start);
$start=STROTIME(“+1天,$start”);
}
而($start<$end);
返回$range;
}
来源:函数createDateRangeArray($strDateFrom,$strDateTo)
{
//获取格式为YYYY-MM-DD的两个日期并创建
//起始日期和结束日期之间的包含日期数组。
//可以在这里测试日期的有效性,但我已经在做了
//在主剧本里
$aryRange=[];
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2),substr($strDateTo,8,2),substr($strDateTo,0,4));
如果($iDateTo>=$iDateFrom){
数组推送($aryRange,date($Y-m-d',$iDateFrom));//第一个条目
在($iDateFrom中,您还可以查看课程:
这将获得一个包含DateTime对象的数组
迭代
foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}
这是非常灵活的
/**
* 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;
}
/**
*在两个日期之间创建日期集合
*
*
*带有日期时间对象的PHP5.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;
}
$日期包含:
Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )
这是短的,甜的,应该在PHP4+中工作
function getDatesFromRange($start, $end){
$dates = array($start);
while(end($dates) < $end){
$dates[] = date('Y-m-d', strtotime(end($dates).' +1 day'));
}
return $dates;
}
函数getDatesFromRange($start,$end){
$dates=数组($start);
while(结束($dates)<$end){
$dates[]=日期('Y-m-d',标准时间(结束($dates)。'+1天');
}
返回$日期;
}
请注意,文斯提供的答案不包括该期间的结束日期。
如果您使用的是PHP 5.3+,最好使用如下函数:
/**
* Generate an array of string dates between 2 dates
*
* @param string $start Start date
* @param string $end End date
* @param string $format Output format (Default: Y-m-d)
*
* @return array
*/
function getDatesFromRange($start, $end, $format = 'Y-m-d') {
$array = array();
$interval = new DateInterval('P1D');
$realEnd = new DateTime($end);
$realEnd->add($interval);
$period = new DatePeriod(new DateTime($start), $interval, $realEnd);
foreach($period as $date) {
$array[] = $date->format($format);
}
return $array;
}
然后,您将按预期调用该函数:
getDatesFromRange('2010-10-01', '2010-10-05');
关于DatePeriod
class的注意事项:可以使用DatePeriod的第四个参数来排除开始日期(DatePeriod::exclude_start_date
),但此时不能包括结束日期。函数getWeekdayDatesFrom($format,$start_date_epoch,$end_date_epoch,$range){
function getWeekdayDatesFrom($format, $start_date_epoch, $end_date_epoch, $range) {
$dates_arr = array();
if( ! $range) {
$range = round(abs($start_date_epoch-$end_date_epoch)/86400) + 1;
} else {
$range = $range + 1; //end date inclusive
}
$current_date_epoch = $start_date_epoch;
for($i = 1; $i <= $range; $i+1) {
$d = date('N', $current_date_epoch);
if($d <= 5) { // not sat or sun
$dates_arr[] = "'".date($format, $current_date_epoch)."'";
}
$next_day_epoch = strtotime('+'.$i.'day', $start_date_epoch);
$i++;
$current_date_epoch = $next_day_epoch;
}
return $dates_arr;
}
$dates_arr=array();
如果(!$范围){
$range=round(abs($start\u date\u epoch-$end\u date\u epoch)/86400)+1;
}否则{
$range=$range+1;//包括结束日期
}
$current\u date\u epoch=$start\u date\u epoch;
对于($i=1;$i,这里有一个函数,它将返回两个方向上的日期范围,并在以下情况下工作:
函数createRange($start,$end,$format='Y-m-d'){
$start=新日期时间($start);
$end=新日期时间($end);
$invert=$start>$end;
$dates=array();
$dates[]=$start->format($format);
while($start!=$end){
$start->modify($invert?'-':'+').“1天”);
$dates[]=$start->format($format);
}
返回$日期;
}
使用示例:
print_r(createRange('2010-10-01','2010-10-05');
/*排列
(
[0] => 2010-10-01
[1] => 2010-10-02
[2] => 2010-10-03
[3] => 2010-10-04
[4] => 2010-10-05
)*/
打印(createRange('2010-10-05','2010-10-01','j M Y');
/*排列
(
[0]=>2010年10月5日
[1] =>2010年10月4日
[2] =>2010年10月3日
[3] =>2010年10月2日
[4] =>2010年10月1日
)*/
实现这一点的方法很多,但最终都取决于您使用的PHP版本。以下是所有解决方案的摘要:
获取PHP版本:
getDatesFromRange( '2010-10-01', '2010-10-05' );
Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )
echo phpinfo();
PHP5.3+
$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);
/**
* 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+
$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);
/**
* 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;
}
/**
*在两个日期之间创建
*@param字符串自
*@param字符串直到
*@param字符串步长
*@param字符串日期格式
*@return数组
*@作者阿里·奥古尔
*/
函数日期范围($first,$last,$step='+1天',$format='d/m/Y'){
$dates=array();
$current=strottime($first);
$last=strottime$last;
while($currentpublic静态函数countDays($date1,$date2)
{
$date1=strotime($date1);//或者您的日期
$date2=标准时间($date2);
$datediff=$date1-$date2;
返回楼层($datediff/(60*60*24));
}
公共静态函数dateRange($date1,$date2)
{
$count=static::countDays($date1,$date2)+1;
$dates=array();
对于($i=0;$i)
它将返回如下数组:
Array
(
[0] => 2014-09-16
[1] => 2014-09-17
[2] => 2014-09-18
[3] => 2014-09-19
[4] => 2014-09-20
[5] => 2014-09-21
[6] => 2014-09-22
[7] => 2014-09-23
[8] => 2014-09-24
[9] => 2014-09-25
[10] => 2014-09-26
)
这里有一种使用碳的方法:
公共函数buildDateRangeArray($first,$last)
{
while($first toDateString();
$first->addDay();
}
返回$日期;
}
当然,这可以调整为不使用Carbon。传递给函数的$first和$last参数是Carbon实例。$report\u start\u date=date('2014-09-16');
$report_starting_date=date('2014-09-16');
$report_ending_date=date('2014-09-26');
$report_starting_date1=date('Y-m-d',strtotime($report_starting_date.'-1 day'));
while (strtotime($report_starting_date1)<strtotime($report_ending_date))
{
$report_starting_date1=date('Y-m-d',strtotime($report_starting_date1.'+1 day'));
$dates[]=$report_starting_date1;
}
print_r($dates);
// dates ('2014-09-16', '2014-09-26')
//print result Array
(
[0] => 2014-09-16
[1] => 2014-09-17
[2] => 2014-09-18
[3] => 2014-09-19
[4] => 2014-09-20
[5] => 2014-09-21
[6] => 2014-09-22
[7] => 2014-09-23
[8] => 2014-09-24
[9] => 2014-09-25
[10] => 2014-09-26
)
$report_ending_date=日期('2014-09-26');
$report_start_date1=日期('Y-m-d',strottime($report_start_date.-1天));
而(标准时间($report\u Start\u date1)2014-09-16
[1] => 2014-09-17
[2] => 2014-09-18
[3] => 2014-09-19
[4] => 2014-09-20
[5] => 2014-09-21
[6] => 2014-09-22
[7] => 2014-09-23
[8] => 2014-09-24
[9] => 2014-09-25
[10] => 2014-09-26
)
简单但像一个符咒:
$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();
$period=new DatePeriod(新日期时间('2015-01-01')、新日期间隔('P1D')、新日期时间('2015-01-15+1天');
foreach($期间为$日期){
$dates[]=$date->格式(“Y-m-d”);
}
//只显示
回声';
var_dump(日期);
回声';
退出();
函数日期介于($date1,$date2)之间
{
$dates=array();
对于($i=$date1)
我认为这是最短的答案
根据需要编辑代码
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') );
for($x=strotime('2015-12-01');$x为了让Mostafa的答案完整,这绝对是最简单、最有效的方法:
function getDatesFromRange($a,$b,$x=0,$dates=[]){
while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
return $dates;
}
var_export(getDatesFromRange('2010-10-01','2010-10-05'));
函数getDatesFromRange($start\u date,
$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 datesbetween ($date1,$date2)
{
$dates= array();
for ($i = $date1
; $i<= $date1
; $i=date_add($i, date_interval_create_from_date_string('1 days')) )
{
$dates[] = clone $i;
}
return $dates;
}
for ($x=strtotime('2015-12-01');$x<=strtotime('2015-12-30');$x+=86400)
echo date('Y-m-d',$x);
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') );
function getDatesFromRange($a,$b,$x=0,$dates=[]){
while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
return $dates;
}
var_export(getDatesFromRange('2010-10-01','2010-10-05'));
function getDatesFromRange($a,$b,$x=0,$dates=[]){
while(end($dates)!=$b && $x=-array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
return $dates;
}
var_export(getDatesFromRange('2010-10-05','2010-10-01'));
function getDatesFromRange($a,$b,$format='Y-m-d',$dates=[],$x=0){
while(date($format,strtotime(end($dates)))!=date($format,strtotime($b)) && $x=array_push($dates,date($format,strtotime("$a +$x day"))));
return $dates;
}
$formats=array("Computer"=>'Y-m-d',"American"=>'m/d/Y','Non-American'=>'d-m-Y');
$start='15-02-2017'; // Non-American formatted start date
$end='2017-02-27'; // Computer formatted start date
foreach($formats as $label=>$format){
echo "<br>$label<br>";
var_export(getDatesFromRange($start,$end,$format));
echo "<br>";
}
Computer
array ( 0 => '2017-02-15', 1 => '2017-02-16', 2 => '2017-02-17', 3 => '2017-02-18',
4 => '2017-02-19', 5 => '2017-02-20', 6 => '2017-02-21', 7 => '2017-02-22',
8 => '2017-02-23', 9 => '2017-02-24', 10 => '2017-02-25', 11 => '2017-02-26',
12 => '2017-02-27', )
American
array ( 0 => '02/15/2017', 1 => '02/16/2017', 2 => '02/17/2017', 3 => '02/18/2017',
4 => '02/19/2017', 5 => '02/20/2017', 6 => '02/21/2017', 7 => '02/22/2017',
8 => '02/23/2017', 9 => '02/24/2017', 10 => '02/25/2017', 11 => '02/26/2017',
12 => '02/27/2017', )
Non-American
array ( 0 => '15-02-2017', 1 => '16-02-2017', 2 => '17-02-2017', 3 => '18-02-2017',
4 => '19-02-2017', 5 => '20-02-2017', 6 => '21-02-2017', 7 => '22-02-2017',
8 => '23-02-2017', 9 => '24-02-2017', 10 => '25-02-2017', 11 => '26-02-2017',
12 => '27-02-2017', )
function getDatesFromRange($start, $end, $format='Y-m-d') {
return array_map(function($timestamp) use($format) {
return date($format, $timestamp);
},
range(strtotime($start) + ($start < $end ? 4000 : 8000), strtotime($end) + ($start < $end ? 8000 : 4000), 86400));
}
date_default_timezone_set('Europe/Berlin');
print_r(getDatesFromRange( '2016-7-28','2016-8-2' ));
print_r(getDatesFromRange( '2016-8-2','2016-7-28' ));
print_r(getDatesFromRange( '2016-10-28','2016-11-2' ));
print_r(getDatesFromRange( '2016-11-2','2016-10-28' ));
print_r(getDatesFromRange( '2016-4-2','2016-3-25' ));
print_r(getDatesFromRange( '2016-3-25','2016-4-2' ));
print_r(getDatesFromRange( '2016-8-2','2016-7-25' ));
print_r(getDatesFromRange( '2016-7-25','2016-8-2' ));
Array ( [0] => 2016-07-28 [1] => 2016-07-29 [2] => 2016-07-30 [3] => 2016-07-31 [4] => 2016-08-01 [5] => 2016-08-02 )
Array ( [0] => 2016-08-02 [1] => 2016-08-01 [2] => 2016-07-31 [3] => 2016-07-30 [4] => 2016-07-29 [5] => 2016-07-28 )
Array ( [0] => 2016-10-28 [1] => 2016-10-29 [2] => 2016-10-30 [3] => 2016-10-31 [4] => 2016-11-01 [5] => 2016-11-02 )
Array ( [0] => 2016-11-02 [1] => 2016-11-01 [2] => 2016-10-31 [3] => 2016-10-30 [4] => 2016-10-29 [5] => 2016-10-28 )
Array ( [0] => 2016-04-02 [1] => 2016-04-01 [2] => 2016-03-31 [3] => 2016-03-30 [4] => 2016-03-29 [5] => 2016-03-28 [6] => 2016-03-27 [7] => 2016-03-26 [8] => 2016-03-25 )
Array ( [0] => 2016-03-25 [1] => 2016-03-26 [2] => 2016-03-27 [3] => 2016-03-28 [4] => 2016-03-29 [5] => 2016-03-30 [6] => 2016-03-31 [7] => 2016-04-01 [8] => 2016-04-02 )
Array ( [0] => 2016-08-02 [1] => 2016-08-01 [2] => 2016-07-31 [3] => 2016-07-30 [4] => 2016-07-29 [5] => 2016-07-28 [6] => 2016-07-27 [7] => 2016-07-26 [8] => 2016-07-25 )
Array ( [0] => 2016-07-25 [1] => 2016-07-26 [2] => 2016-07-27 [3] => 2016-07-28 [4] => 2016-07-29 [5] => 2016-07-30 [6] => 2016-07-31 [7] => 2016-08-01 [8] => 2016-08-02 )
$first = '10/30/2017'; //starting date
$last= '10/11/2017'; //ending date
$first_time_arr=explode('/',$first);
$last_time_arr=explode('/',$last);
//create timestamp of starting date
$start_timestamp=mktime(0,0,0, $first_time_arr[0], $first_time_arr[1],$first_time_arr[2]);
//create timestamp of ending date
$end_timestamp=mktime(0,0,0, $last_time_arr[0], $last_time_arr[1],$last_time_arr[2]);
$date_arr=array();
for($i=$start_timestamp;$i<=$end_timestamp;$i=$i+86400){
$date_arr[]=date("Y-m-d",$i); //this will save all dates in array
}
// Specify the start date. This date can be any English textual format
$date_from = "2018-02-03";
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = "2018-09-10";
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp
// Loop from the start date to end date and output all dates inbetween
for ($i=$date_from; $i<=$date_to; $i+=86400) {
echo date("Y-m-d", $i).'<br />';
}
$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>";
}