简体   繁体   English

如何使用 for 或 foreach 循环运行 DateTime 以循环每一天并为每个循环添加 +3 天?

[英]How can I run a DateTime with for or foreach loop to loop through each day and add +3 days to each?

I have 2 dates in PHP, how can I run a for or foreach loop to loop through each day and add +3 days to each?我在 PHP 中有 2 个日期,我如何运行一个 for 或 foreach 循环来循环每一天并添加 +3 天?

below I add an example of how I want to get the listing.下面我添加了一个如何获取列表的示例。

  <?php
  $begin = new DateTime( "2022-01-01" );
  $end   = new DateTime( "2022-01-31" );
  for($i=$begin; $i<=$end; $i->modify('+1 day')){    
    for($j=$begin; $j<=$end; $j->modify('+3 day')){
      
      echo $i->format("M d, Y").' => ';
      echo $j->format("M d, Y").'<br/><br/>';
      
      /*  I WANTS TO GET

      * Jan 01, 2022 => Jan 04, 2022
      * Jan 02, 2022 => Jan 05, 2022
      * Jan 03, 2022 => Jan 06, 2022
      *...
      *...
      * Jan 31, 2022 => Feb 03, 2022
      */

    }    
  }    
?>

Using the DateInterval() and DatePeriod() its a simple case of setting the parameters and running a loop使用DateInterval()DatePeriod()是设置参数和运行循环的简单案例

$begin = new DateTime( "2022-01-01" );
$end   = new DateTime( "2022-01-31" );

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $dateFrom){
    $to = clone $dateFrom;
    echo sprintf("%s => %s\n", 
                    $dateFrom->format("M d, Y"), 
                    $to->modify('3 day')->format("M d, Y")
        );  
}

RESULT结果

Jan 01, 2022 => Jan 04, 2022
Jan 02, 2022 => Jan 05, 2022
Jan 03, 2022 => Jan 06, 2022
Jan 04, 2022 => Jan 07, 2022
Jan 05, 2022 => Jan 08, 2022
Jan 06, 2022 => Jan 09, 2022
Jan 07, 2022 => Jan 10, 2022
Jan 08, 2022 => Jan 11, 2022
Jan 09, 2022 => Jan 12, 2022
Jan 10, 2022 => Jan 13, 2022
Jan 11, 2022 => Jan 14, 2022
Jan 12, 2022 => Jan 15, 2022
Jan 13, 2022 => Jan 16, 2022
Jan 14, 2022 => Jan 17, 2022
Jan 15, 2022 => Jan 18, 2022
Jan 16, 2022 => Jan 19, 2022
Jan 17, 2022 => Jan 20, 2022
Jan 18, 2022 => Jan 21, 2022
Jan 19, 2022 => Jan 22, 2022
Jan 20, 2022 => Jan 23, 2022
Jan 21, 2022 => Jan 24, 2022
Jan 22, 2022 => Jan 25, 2022
Jan 23, 2022 => Jan 26, 2022
Jan 24, 2022 => Jan 27, 2022
Jan 25, 2022 => Jan 28, 2022
Jan 26, 2022 => Jan 29, 2022
Jan 27, 2022 => Jan 30, 2022
Jan 28, 2022 => Jan 31, 2022
Jan 29, 2022 => Feb 01, 2022
Jan 30, 2022 => Feb 02, 2022

If the end date is always simply three days from the start, then you only need one loop, over the start date.如果结束日期总是距开始日期仅三天,那么您只需要一个循环,超过开始日期。 Then, instead of a second loop to set the ending date, just set it relative to the start, on each iteration:然后,不是第二个循环来设置结束日期,而是在每次迭代时将其设置为相对于开始日期:

for ($start = $begin; $start <= $end; $start->modify('+1 day')) {    
    $stop = clone $start;
    $stop->modify('+3 days');
    printf("%s => %s\n", $start->format("M d, Y"), $stop->format("M d, Y"));
}

Note you need to make a new date object via clone so that your second modification of +3 days doesn't change the start date.请注意,您需要通过克隆创建一个新日期 object,以便您的第二次修改+3 days不会更改开始日期。

You can also just use the same DateTime object for all of it if you like.如果您愿意,您也可以只使用相同的 DateTime object。

for ($i = $begin; $i <= $end; $i->modify('-2 day')) {
    echo $i->format("M d, Y") . ' => ';
    echo $i->modify('+3 day')->format("M d, Y") . '<br><br>';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在foreach循环中为每个项目添加一个随机数,但要保持1天不变 - How can I add a random number to each item in foreach loop but to stay the same for 1 day 我在 PHP 中有 2 个日期,如何运行一个 foreach 循环来经历所有这些日子? - I have 2 dates in PHP, how can I run a foreach loop to go through all of those days? 如何使用array_push在PHP / Laravel中通过数据库查询结果在foreach循环中添加键值对以标记返回的每一行? - How can I use array_push to add a key value pair in a foreach loop through database query results in PHP/Laravel to flag each row returned? 我在PHP中有2次时间,如何运行foreach循环来遍历所有这些日子? (最后24小时同时循环1小时!) - I have 2 time in PHP, how can I run a foreach loop to go through all of those days? (1 Hour loop same time Last 24 Hours!) 我如何找出foreach循环中每个if条件执行一个循环的次数? - How can i find out how many times a loop was executed in each if condition inside foreach loop? 如何为每个循环将我的JavaScript添加到我的php中? - How can I add my javascript to my php for each loop? 如何在foreach / for循环中每次添加超过1个? - How to add more than 1 each time in a foreach/for loop? 在for循环中显示一个月中每天的记录数,以显示该月的日期 - Displaying number of records for each day in a month in a for loop to display the days of the month 如何回显 foreach 循环的每个值? - How do I echo each value of a foreach loop? 我如何规范化/检查它可以通过foreach循环(php)运行的json对象输出 - How can I normalize/check a json object output it can run through a foreach loop (php)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM