date ('dmY', strtotime ('10 .08.2010 ') + 667 * 86400);

It works incorrectly gives the answer 07.06.2012 and should be 03.08.2012

  • Do you want to add days or years? - sanmai
  • @sanmai, 86400 is a day, that is, a day. - Visman
  • For example: strtotime('+3 days'); - And
  • one
    Why do you think the answer is wrong? - sanmai
  • Correct answer. - Enikeyschik

2 answers 2

It is possible through the well-known library Carbon

 use Carbon\Carbon; // К указанной дате добавить год и вывести в формате даты echo Carbon::parse('10.08.2010')->addYears(1)->toDateString(); 

It is very flexible, you can add through the methods:

 ->addHours() // Часы ->addDays() // Дни ->addYears() // Года // и т.д. 

PHP Carbon Library Documentation

    If you take into account the figures that you gave, you can do this:

     $date = new DateTime('10.08.2010'); $date->add(new DateInterval('P1Y359D')); echo $date->format('dmY'); 

    The result will be the required date.

     03.08.2012 

    After discussion with @ Enikeyschik decided to add a couple of points. My calculations, as you could understand, are repelled from the date of 10.08.2010 and are completed on the date indicated at the end of the post on 03.08.2012.

    Another moment. Unfortunately, the author did not specify: this is a specific date or there is a certain array with different dates for which the calculation takes place. In the latter case, the approach to the formation of the result based on the number of days + the original date is fundamentally wrong, because there may be 366 days in a year. In this case, in my opinion, it is necessary to add "2 years" and subtract 6 days from here.

    • Where did the P1Y359D come from? - Enikeyschik
    • @ Enikeyschik 1Y - 1 year, 359D - respectively 359 days. Expansion description php.net/manual/ru/datetime.add.php - Rustin
    • I understand what this means. In question 667 days. Where did you get the year and 359 days? - Enikeyschik
    • @ Enikeyshchik The question states "but it should be 08/03/2012", respectively, from the date 08/10/2010 - Rustin
    • So maybe the author is just quite short? No need to customize the decision to answer, perhaps the answer is wrong. - Enikeyschik