简体   繁体   中英

Given start date get all the months name until current month?

Given start date how to get all the months until current month?

For eg. If given date is 1st Jan 2019

output should be:

Jan 2019

Feb 2019

Mar 2019

Apr 2019

May 2019

Jun 2019

Jul 2019

Aug 2019

Sep 2019

Oct 2019

Nov 2019

Dec 2019

Jan 2020

Feb 2020

Mar 2020

Solved it with slightly different approach. Thank you all for hints and answers.

<?php 

$startTime = strtotime("1 January 2019");
$startYear = date("Y", $startTime);
$currentYear = date("Y");
$yearDiff = $currentYear - $startYear;

$currentMonth = date("m", time());

for($i=0; $i < intval($currentMonth) + ($yearDiff * 12) ; $i++) 
{
$t = strtotime("+". $i . " months", $startTime);
$monthName = date("M", $t) ." ". date('Y', $t);
echo $monthName . "<br/>\n";
} 
?>

here you go , hope this work

$start    = new DateTime('2019-01-01');

$end      = new DateTime('2020-03-02');

$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
echo $dt->format("F Y") . "<br>\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM