从PHP中的定义日期开始递增日期

时间:2019-05-24 19:50:05

标签: php loops date

如何在给定的日期上连续增加一个月(如连续循环),

请参见下面的Scenero

given this $original_date is  30-7-2019   ,  echo 30-8-2019        //$original_date  + 1

when current date is > 30-8-2019 ,    echo 30-9-2019  i.e add one month to last
when current date is > 30-9-2019 ,    echo 30-10-2019 i.e add one month to last
when current date is > 30-10-2019 , echo 30-11-2019 i.e add one month to last
when current date is > 30-11-2019 , echo 30-12-2019 i.e add one month to last

以此类推

我得到了当前日期和给定日期之间的差额,并且总是将差额加到每个当前日期

$today = date('Y-m-d');
$given_date= date('2019-6-29');
//echo $date = date('Y-m-d', strtotime('+1 month', strtotime($given_date)));

//get month difference function
function getyearDiff($date){
            $today = new DateTime();
            $date = new DateTime($date);
            $interval = $date->diff($today);
            return $interval->format("%r%m");   
        }

//get month difference and remove negative if it exist
$diif =  abs(getyearDiff($filling_period)) ;
//echo $diif;

//add the difference
echo $date = date('Y-m-d', strtotime('+'.$diif.' month', strtotime($filling_period)));

总是从给定日期到预期日期增加一个月

1 个答案:

答案 0 :(得分:1)

Demo Link

这是您可以尝试的摘录

$today = new DateTime();
$given_date= new DateTime('2019-06-29');
$period = new DatePeriod( // create period instance between today and given date
     $today,
     new DateInterval('P1M'), // continously add one month difference
     $given_date
);
foreach( $period as $date) { 
   echo $date->format('Y-m-d').'<br/>'; 
}
print_r($array);