2016-07-30 12 views

Antwort

1

Einfache while Schleife:

$today = new DateTime('today'); 
$input = new DateTime('2014-06-19'); 

while ($today > $input) { 
    $input->modify('+6 months'); 
} 

echo $input->format('Y-m-d'); // 2016-12-19 

demo

0

Die Lösung DateTime und DateInterval Objekte mit:

$input_date = new \DateTime("2014-06-19"); 
$six_months = new \DateInterval("P6M"); 
$now = new \DateTime(); 
$limit = (new \DateTime())->sub($six_months); 

while ($input_date < $now && $input_date <= $limit) { 
    $input_date->add($six_months); 
} 

print_r($input_date->format("Y-m-d")); // 2016-06-19 
+0

'2016-06-19' ist nicht größer als heute. Oder habe ich OP falsch verstanden? –

+0

in Ihrem Fall '2016-06-19' sollte der" maximal zulässige "Datumswert sein, da der nächste Wert' 2016-12-19' kommt nachdem er größer ist als heute. Also sollten wir den Loop stoppen bei '2016-06-19' – RomanPerekhrest

+0

Aber' 2016-06-19' ist nicht größer als heute. Anscheinend verstehe ich OPs Bedürfnisse nicht. Mal sehen, ob er seine Frage aktualisiert –

-1
<?php 
echo date("Y-m-d", strtotime("2009-01-31 +1 month")); // PHP: 2009-03-03 
echo date("Y-m-d", strtotime("2009-01-31 +2 month")); // PHP: 2009-03-31 
?> 

php.net month issue

-1
$datetime = new DateTime(); 
$datetime->modify('+6 months'); 
echo $datetime->format('d'); 

oder

oder in PHP Version 5.4+

echo (new DateTime())->add(new DateInterval('P6M'))->format('d'); 
Verwandte Themen