我在PHP中有2个日期,我如何运行foreach循环来遍历所有这些日子?

我以日期2010-05-01开始,以2010-05-10结束。如何在PHP中遍历所有这些日期?

220005 次浏览

转换为unix时间戳使在php中计算日期更容易:

$startTime = strtotime( '2010-05-01 12:00' );
$endTime = strtotime( '2010-05-10 12:00' );


// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
$thisDate = date( 'Y-m-d', $i ); // 2010-05-01, 2010-05-02, etc
}

当使用带有夏令时的时区时,请确保添加的时间不是23:00、00:00或1:00,以防止跳过或重复日期。

$startTime = strtotime('2010-05-01');
$endTime = strtotime('2010-05-10');


// Loop between timestamps, 1 day at a time
$i = 1;
do {
$newTime = strtotime('+'.$i++.' days',$startTime);
echo $newTime;
} while ($newTime < $endTime);

$startTime = strtotime('2010-05-01');
$endTime = strtotime('2010-05-10');


// Loop between timestamps, 1 day at a time
do {
$startTime = strtotime('+1 day',$startTime);
echo $startTime;
} while ($startTime < $endTime);
$begin = new DateTime('2010-05-01');
$end = new DateTime('2010-05-10');


$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);


foreach ($period as $dt) {
echo $dt->format("l Y-m-d H:i:s\n");
}

这将输出在$start$end之间定义的时间段内的所有天数。如果你想包含第10个,将$end设置为11。你可以根据自己的喜好调整格式。关于DatePeriod,请参阅PHP手册。它需要PHP 5.3。

用户本功能:-

function dateRange($first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);


while( $current <= $last ) {
$dates[] = date($format, $current);
$current = strtotime($step, $current);
}
return $dates;
}

使用/函数调用:-

增加一天:-

dateRange($start, $end); //increment is set to 1 day.

按月递增:-

dateRange($start, $end, "+1 month");//increase by one month

如果你想设置日期格式,可以使用第三个参数:-

   dateRange($start, $end, "+1 month", "Y-m-d H:i:s");//increase by one month and format is mysql datetime

这也包括最后的日期

$begin = new DateTime( "2015-07-03" );
$end   = new DateTime( "2015-07-09" );


for($i = $begin; $i <= $end; $i->modify('+1 day')){
echo $i->format("Y-m-d");
}

如果你不需要最后的日期,只需从条件中删除=

这里有一个方法:

 $date = new Carbon();
$dtStart = $date->startOfMonth();
$dtEnd = $dtStart->copy()->endOfMonth();


$weekendsInMoth = [];
while ($dtStart->diffInDays($dtEnd)) {


if($dtStart->isWeekend()) {
$weekendsInMoth[] = $dtStart->copy();
}


$dtStart->addDay();
}

$weekendsInMoth的结果是周末天数的数组!

包容范围的php.net样本:

$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2012-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>";
}

下面是另一个简单的实现

/**
* Date range
*
* @param $first
* @param $last
* @param string $step
* @param string $format
* @return array
*/
function dateRange( $first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
$dates = [];
$current = strtotime( $first );
$last = strtotime( $last );


while( $current <= $last ) {


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


return $dates;
}

例子:

print_r( dateRange( '2010-07-26', '2010-08-05') );


Array (
[0] => 2010-07-26
[1] => 2010-07-27
[2] => 2010-07-28
[3] => 2010-07-29
[4] => 2010-07-30
[5] => 2010-07-31
[6] => 2010-08-01
[7] => 2010-08-02
[8] => 2010-08-03
[9] => 2010-08-04
[10] => 2010-08-05
)
$date = new DateTime($_POST['date']);
$endDate = date_add(new DateTime($_POST['date']),date_interval_create_from_date_string("7 days"));


while ($date <= $endDate) {
print date_format($date,'d-m-Y')." AND END DATE IS : ".date_format($endDate,'d-m-Y')."\n";
date_add($date,date_interval_create_from_date_string("1 days"));
}
你也可以这样迭代,$_POST['date']可以从你的应用程序或网站中被削弱 除了$_POST['date'],你也可以把你的字符串放在这里"21-12-2019"

如果你使用Laravel并且想要使用Carbon,正确的解决方案如下:

$start_date = Carbon::createFromFormat('Y-m-d', '2020-01-01');
$end_date = Carbon::createFromFormat('Y-m-d', '2020-01-31');


$period = new CarbonPeriod($start_date, '1 day', $end_date);


foreach ($period as $dt) {
echo $dt->format("l Y-m-d H:i:s\n");
}

记得加上:

  • 使用碳\碳;
  • 使用碳\ CarbonPeriod;
<?php


$start_date = '2015-01-01';
$end_date = '2015-06-30';


while (strtotime($start_date) <= strtotime($end_date)) {
echo "$start_daten";
$start_date = date ("Y-m-d", strtotime("+1 days", strtotime($start_date)));
}


?>

对于Carbon用户

use Carbon\Carbon;


$startDay = Carbon::parse("2021-08-01");
$endDay= Carbon::parse("2021-08-05");
$period = $startDay->range($endDay, 1, 'day');

当我打印数据时

[
Carbon\Carbon @1627790400 {#4970
date: 2021-08-01 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon @1627876800 {#4974
date: 2021-08-02 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon @1627963200 {#4978
date: 2021-08-03 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon @1628049600 {#5007
date: 2021-08-04 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon @1628136000 {#5009
date: 2021-08-05 00:00:00.0 America/Toronto (-04:00),
},
]

这是Laravel数据转储使用dd($period->toArray());。你现在可以通过foreach语句遍历$period

重要提示 -它包括提供给方法的两个边缘日期。

想要了解更多与约会相关的东西,请查看碳文档