Home > OS >  How can I run a DateTime with for or foreach loop to loop through each day and add 3 days to each?
How can I run a DateTime with for or foreach loop to loop through each day and add 3 days to each?

Time:12-07

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?

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
      */

    }    
  }    
?>

CodePudding user response:

Using the DateInterval() and DatePeriod() its a simple case of setting the parameters and running a loop

$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

CodePudding user response:

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.

CodePudding user response:

You can also just use the same DateTime object for all of it if you like.

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>';
}
  • Related