user3909841
user3909841

Reputation: 23

php for loop for date increment each time

I want to loop a date so that every time date is increment by previous date. my code is here. plz reply anyone, thanks in advance

  $today = date('Y-m-d'); 

  for($i=1; $i<=4; $i++){                
    $repeat = strtotime("+2 day",strtotime($today));
    echo $rdate = date('Y-m-d',$repeat);
  }

I want result as if today is 2016-04-04 than, 2016-04-06, 2016-04-08, 2016-04-10, 2016-04-12.

actually i want to make a reminder date where user enter reminder. lets a user want to add reminder today and want repeat it 5 time after 2days, 3days or what ever he wants, in next comming day. than how i repeat date with for loop.

Upvotes: 1

Views: 9246

Answers (5)

Soteris 92
Soteris 92

Reputation: 121

The easiest way is what answer

aslawin

The below example is to go through the date

  $begin = new DateTime($check_in);
  $end =  new DateTime($check_out);

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

  foreach ($period as $dt)
  {
      <sample code here>
  }

Upvotes: 2

Mikey
Mikey

Reputation: 2704

actually i want to make a reminder date where user enter reminder. lets a user want to add reminder today and want repeat it 5 time after 2days, 3days or what ever he wants, in next comming day. than how i repeat date with for loop.

I'll help with the above. First of all I will just say I have a huge personal preference towards the DateTime object over simply using date it's more flexible and a hell of a lot more readable in my opinion, so when working with dates I would always suggest using that over date()

So here is some Code:

$date = new DateTime(); // Pretend this is what the User entered. We got it via $_POST or something.
$interval = 2; // Repeat x times at y day intervals. (Not including the initial)
$repeatAmount = 2; // Repeat the reminder x times

for ($i = 0; $i <= $repeatAmount; ++$i) {
    echo $date->format('d/m/Y');    
    $date->modify('+'. $interval .' day');
}

$date = new DateTime()Imagine this is the date the user entered, this is our starting point, our first reminder will at this time.

$interval and $repeatAmount are the interval in days, i.e. I want this to every 2 days and the amount of times you want it to repeat, in our example 2.

for ($i = 0; $i <= $repeatAmount; ++$i) { We want to loop as many times as the user says they want to repeat. Little note ++$i tends to be a very minor performance boost over $i++ in some scenarios, so it is usually better to default to that unless you specifically need to use $i++

echo $date->format('d/m/Y'); Simply print out the date, i'll let you handle the reminder logic.

$date->modify('+' . $interval . ' day'); Increment the dateTime object by the interval that the user has asked for, in our case increment by 2 days.

Any questions let me know.

Upvotes: 0

fusion3k
fusion3k

Reputation: 11689

Use a for loop with base 2, then directly output your dates:

for( $i=2; $i<9; $i=$i+2 )
{
    echo date('Y-m-d', strtotime( "+ $i days" )) . PHP_EOL;
}

Result:

2016-04-06
2016-04-08
2016-04-10
2016-04-12

Upvotes: 0

aslawin
aslawin

Reputation: 1981

Try this:

<?php

$today = date('Y-m-d'); 

for($i=1; $i<=4; $i++)
{
    $repeat = strtotime("+2 day",strtotime($today));
    $today = date('Y-m-d',$repeat);
    echo $today;
}

Output:

2016-04-06
2016-04-08
2016-04-10
2016-04-12

Upvotes: 5

devpro
devpro

Reputation: 16117

You can try this:

$today = date('Y-m-d'); 
for($i=1; $i<=8; $i++){
   if($i%2 == 0){
      $repeat = strtotime("+$i day",strtotime($today));
      echo $rdate = date('Y-m-d',$repeat);
   }  
}

Result:

2016-04-06
2016-04-08
2016-04-10
2016-04-12

In this example, you can use $i%2 == 0 with limit <= 8

Upvotes: 0

Related Questions