我在PHP中有两个日期,如何运行foreach循环来度过所有这些日子?

我在PHP中有两个日期,如何运行foreach循环来度过所有这些日子?,php,foreach,date,Php,Foreach,Date,我以日期2010-05-01开始,以日期2010-05-10结束。如何在PHP中迭代所有这些日期?转换为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 <= $e

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

转换为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
}
$startTime=strotime('2010-05-01 12:00');
$endTime=strottime('2010-05-10 12:00');
//在时间戳之间循环,一次24小时
对于($i=$startTime;$i
$startTime=strotime($2010-05-01));
$endTime=strottime('2010-05-10');
//在时间戳之间循环,每次1天
$i=1;
做{
$newTime=strottime('+'.$i++.'days',$startTime);
echo$newTime;
}而($newTime<$endTime);

$startTime=strotime('2010-05-01');
$endTime=strottime('2010-05-10');
//在时间戳之间循环,每次1天
做{
$startTime=strotime(“+1天”,$startTime);
echo$startTime;
}而($startTime<$endTime);
这将输出定义期间内
$start
$end
之间的所有天。如果要包括第10天,请将
$end
设置为第11天。您可以根据自己的喜好调整格式。有关详细信息,请参阅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, "+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");
}
$begin=新日期时间(“2015-07-03”);
$end=新日期时间(“2015-07-09”);
对于($i=$begin;$i modify(“+1天”)){
echo$i->格式(“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=新日期时间('2012-08-01');
$end=新日期时间('2012-08-31');
$end=$end->modify(“+1天”);
$interval=新的日期间隔('P1D');
$daterange=新的日期周期($begin,$interval,$end);
foreach($daterange作为$date){
echo$date->format(“Ymd”)。“
”; }
这里是另一个简单的实现-

/**
 * 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;
}
$date=新的日期时间($_POST['date']);
$endDate=date\u add(新的日期时间($\u POST['date']),date\u interval\u create\u from\u date\u string(“7天”);

而($date如果您使用Laravel并希望使用碳,则正确的解决方案如下:

$start_date=Carbon::createFromFormat('Y-m-d','2020-01-01');
$end_date=碳::createFromFormat('Y-m-d','2020-01-31');
$period=新周期($start\u date,$1天,$end\u date);
foreach(期间为$dt){
echo$dt->format(“ly-m-dh:i:s\n”);
}
请记住添加:

  • 使用碳\碳
  • 使用碳\碳周期


我不喜欢86400的外观。我知道它是60*60*24,但仍然…它的某些方面让我感到不舒服。在这种情况下,它可以工作,但如果在正常时间和日光节约时间之间切换,它将失败,因为有90000秒的时间,你的循环中会有两次……迈克,最好的办法是设置一个常数和将其命名为“DAY”,以使其更易于阅读。这将受到夏令时问题的影响。当您跨越夏令时点时,它将出错。12:00am不是时间点两侧的12:00am。此代码会(每个代码每天有86400秒)夏令时有问题!夏令时有些日子只持续23小时,大约25小时。好消息-有一个补丁可以设置一个包含结束日期的标志,该标志(祈祷)将成为未来版本。
$begin->setTime(0,0);$end->setTime(12,0);
或使用开始日期当天的时间初始化,因为任何晚于结束日期的时间都会在循环中包含结束日期。这不是最时髦的修复方法,但只要没有合适的标志,这是最好的选择。如果要在间隔中包含结束日期,可以执行:$end=$end->modify(“+1天”);是否可以使用此选项,但可以将其反转,以回溯历史?@JulienITARD这是一个不错的主意,但更优雅的做法是$end->add($interval),因为它直接响应更改的间隔;)此解决方案似乎比公认的答案慢(运行一些测试台:在60次迭代中100%慢)。但我选择此循环是为了对旧主机平台进行追溯兼容性。请务必注意,
$begin
在循环后会有所不同。此循环修改由
新日期时间(“2015-07-03”)创建的对象
。因此,您应该使用DateTimeImmutable版本。但您需要进一步修改才能使用它们。这是最好、最完整的答案。只是缺少了对DateInterval值P1D的一些解释,因此以下是一些周期指示器示例:两天:P2D两秒:PT2S一周十分钟:p1wty年M表示月D表示天W表示周。这些转换为天,因此不能与D.H表示小时M表示分钟S表示秒合并
   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();
 }
$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"));
}
<?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)));
    }

?>