1

I have code given below:

       $split=explode(' ',$getdate);
       $date=$split['0'];                                   //comming 2018-06-04
        $newdates = date ($date, strtotime ("+".$days) ) ;  //days having 30 value
      // $newdate = date ( 'Y-m-d' , $newdates );         
       $currentdate=date('Y-m-d');
       echo "<pre>";print_r($newdates);

but its not adding the 30 days in the current date can anyone please help me related this

Shruti
  • 149
  • 1
  • 9
  • Where are $getdate & $days coming from. What are you actually trying to output? – Joseph_J Jun 04 '18 at 07:04
  • $split['0'] I think should be $split[0], right? – Albeis Jun 04 '18 at 07:04
  • every thing is working fine but ists not adding the days thats it / after split i am getting the date in Y-m-d – Shruti Jun 04 '18 at 07:05
  • you're using `date()` wrong, here's the line problem `$newdates = date ($date, strtotime ("+".$days) ) ;`, you already know you need to provide the format on the first argument – Kevin Jun 04 '18 at 07:05
  • Possible duplicate of [Adding days to $Date in PHP](https://stackoverflow.com/questions/3727615/adding-days-to-date-in-php) – Nigel Ren Jun 04 '18 at 07:06
  • just use `DateTime` object, then use `$date = new DateTime($split[0]); echo $date->modify('+' . $days . ' days')->format('Y-m-d');` like so, no fuzz or anything else – Kevin Jun 04 '18 at 07:08

2 Answers2

0

I think you need something like

 $newdates = date ('Y-m-d', strtotime ($date . " + " . $days . "days") ) 

or simply

 $newdates = date ('Y-m-d', strtotime ("+ " . $days . "days") ) 
Gerard H. Pille
  • 2,528
  • 1
  • 13
  • 17
0

If your trying to build an array containing the next 30 days, loop over the date() & strtotime() functions like so:

$todaysDate = date('Y-m-d');
for($i = 0; $i < 30; $i++){

  $newDates[] = date('Y-m-d', strtotime($todaysDate .  ' +' . $i . ' days'));

}

echo '<pre>';
print_r($newDates);
Joseph_J
  • 3,654
  • 2
  • 13
  • 22