在每个循环上重置DateTime对象[关闭]

With the code below I get this results

2015-07-15 10:26:12 2015-08-15 10:26:12

2015-08-15 10:26:12 2015-09-15 10:26:12

2015-09-15 10:26:12 2015-10-15 10:26:12

2015-10-15 10:26:12 2015-11-15 10:26:12

Here is what I want to get as result

2015-07-15 10:26:12 2015-08-15 10:26:12

2015-08-15 10:26:12 2015-08-15 10:26:12

2015-07-15 10:26:12 2015-08-15 10:26:12

2015-08-15 10:26:12 2015-08-15 10:26:12

for ($i = 0; $i < 10; $i++) {
   echo  $time->format('Y-m-d h:i:s') . $time->modify('+1 month')->format('Y-m-d h:i:s'); 
}

So is it possible to reset datetime object on every loop?

You could use DateTimeImmutable instead of DateTime.
When you call DateTimeImmutable::modify the instance isn't modified but a new istance with the modified values is returned.

<?php
$time = new DateTimeImmutable('07/15/2015 10:26:12');

for ($i = 0; $i < 10; $i++) {
    echo  $time->format('Y-m-d h:i:s'), ' ', $time->modify('+1 month')->format('Y-m-d h:i:s'), "
";
}

prints

2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12
2015-07-15 10:26:12 2015-08-15 10:26:12