Dexter
Dexter

Reputation: 9314

Custom Month and Year inside php for-loop

I saw this question: how to get the previous 3 months in php

My question is.. How do i output from a custom month.
I want to start from Mar 2018 (or any M Y user inputs) and it should output the next 3 (or any number user inputs) months.
Ex: Mar, Apr, May

The code below is from current month and year.

// current month: Aug 2018
for ($i = 0; $i <= 2; $i++){
    $x = strtotime("$i month");
    echo $dte = date('M Y', $x);
    echo '<br>';
}

And the output is

Aug 2018
Sep 2018
Oct 2018

Upvotes: 0

Views: 828

Answers (6)

DsRaj
DsRaj

Reputation: 2328

You can generate the date with strtotime() function

Check this phpfiddle

<?php
$currMonth = 11;
$currYear  = 2017;
$count = 15;
$currentYear = date('M Y',strtotime($currYear.'-'.$currMonth));
echo $currentYear.'<br>';
for ($i = 1; $i < $count; $i++) {
    $currMonth++;
    if ($currMonth > 12) {
        $currMonth = 1;
        $currYear++;    
    }   
    $newMonthandYear = date('M Y',strtotime($currYear.'-'.$currMonth));
    echo $newMonthandYear.'<br>';
}
?> 

Upvotes: 0

Rahul Gandhi
Rahul Gandhi

Reputation: 19

Try this code for add nth Days, Months and Years

$n = 2;
for ($i = 0; $i <= $n; $i++){
    $d = strtotime("$i days");
    $x = strtotime("$i month");
    $y = strtotime("$i year");
    echo "Dates : ".$dates = date('d M Y', "+$d days");
    echo "<br>";
    echo "Months : ".$months = date('M Y', "+$x months");
    echo '<br>';
    echo "Years : ".$years = date('Y', "+$y years");
    echo '<br>';
}

Upvotes: 0

Matt Jameson
Matt Jameson

Reputation: 582

<?php

$year = date('Y');

//start at march 
$startMonth = 7;

//go forwards 2 months
$stepForwards = 22;    

for ($i = $startMonth; $i <= $stepForwards; $i++){

    if($i > 12 ) {

        $month = $i % 12 == 0 ?  12 : $i % 12;

    }
    else {

       $month = $i;

    }

    echo date("M Y", strtotime(date($year.'-'.$month.'-1'))) . "<br/>";

    if($month == 12) {

        $year++;

        echo "<br/>";

    }

}

Upvotes: -1

Zhorov
Zhorov

Reputation: 29943

This may be also helpful:

<?php
$month = 11;
$year  = 2017;
$count = 15;

for ($i = 1; $i <= $count; $i++) {
    $month++;
    if ($month > 12) {
        $month = 1;
        $year++;    
    }   
    $x = DateTime::createFromFormat('Y-m', $year.'-'.$month);
    echo $x->format('m-Y');
    echo '<br>';
}
?> 

Output:

12-2017
01-2018
02-2018
03-2018
04-2018
05-2018
06-2018
07-2018
08-2018
09-2018
10-2018
11-2018
12-2018
01-2019
02-2019

Upvotes: 1

AymDev
AymDev

Reputation: 7474

You could use the DateTime class and increment using a DateInterval object:

// Assuming these are the user inputs
$month = 11;
$year = 2015;

// We create a new object with year and month format
$date = DateTime::createFromFormat('Y m', $year . ' ' . $month);


for ($i = 0; $i <= 2; $i++){
    // Output the month and year
    echo $date->format('m Y') . '<br>';

    // Add 1 month to the date
    $date->add(new DateInterval('P1M'));
}

Output:

11 2015
12 2015
01 2016

Documentation:

Upvotes: 3

Nikhil Joshi
Nikhil Joshi

Reputation: 386

Change it to this as below it will give you as expected see the below code

    // current month: Aug 2018
  $effective_date = "MAR 2018";
for ($i = 0; $i <= 2; $i++){
    $x = strtotime("$i month",strtotime($effective_date));
    echo $dte = date('M Y', $x);
    echo '<br>';
}

Upvotes: 2

Related Questions