Adding new elements to a PHP array and preserving the previous array size

I have the following code:

$months = array();
$numJoin = date("n",strtotime($me['joinTime']));
$numLast = date('n', strtotime('Dec 31'));
$numCurrent = date("n",strtotime('2016-06-01'));
array_push($months, date("F", strtotime($me['joinTime'])));
for($i = ($numJoin + 1); $i <= $numLast; $i++) {
    if($numCurrent>$numJoin) {
        $dateObj = date_create_from_format('!m', $i);
        array_push($months, $dateObj->format('F'));
    }
    $numCurrent= -1;
}

What I'm trying to do here is to add the array that starts up in the current month and store the previous months in the array, for example, for example:

Beginning of the month → May June starts → I add June to the array (now I should have May and June in the array).

July starts -> I add July to the array (now I should have May, June and July in the array).

How can i do this? The current solution only works for +1 month. I can not add more than 1 month: /

PS A new element should be added only at the entrance of a new month, and the previous contents of the array should be saved ...

+4
2

, , .

$months = array();
$num = date("n",strtotime($me['joinTime'])); //join month number
$now = date("n"); //Current month number

for($i = $num; $i <= $now; $i++){
    $dateObj = DateTime::createFromFormat('!m', $i);
    array_push($months, $dateObj->format('F'));
}
print_r($months);
+1

, , , ... ...

$me = array('joinTime'=>'2016-03-01');

$dtCurrent = strtotime($me['joinTime']);
$arrMonths = array();
while($dtCurrent < time()) {
    $dtCurrent = strtotime("+1 month",$dtCurrent);
    $arrMonths[] = date('F',$dtCurrent);
}

var_dump($arrMonths);
0

All Articles