Manish Malviya
Manish Malviya

Reputation: 871

how to get next four month with year in php?

i want to get the current month and next three months with year in a dropdown box, the proble is when November2012 comes then the last month would be January2013, if the current month is december2012 then the nest three months would be

january2013 february2013 march2013

in the drop down it should look like

December2012
january2013
february2013
march2013

Upvotes: 1

Views: 2415

Answers (5)

Dejan Todorovic
Dejan Todorovic

Reputation: 48

$this_month = mktime(0, 0, 0, date('m'), 1, date('Y'));

for($i=0;$i<4;$i++) {

echo date("M Y", strtotime($i." month", $this_month)) .'
';

}

Upvotes: 0

adrien
adrien

Reputation: 4439

Try something like this:

$this_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
for ($i = 0; $i < 4; ++$i) {
    echo date('M Y', strtotime($i.' month', $this_month)) . '<br/>';
}

Upvotes: 8

h00ligan
h00ligan

Reputation: 1481

If you're feeling a little object oriented:

date_default_timezone_set('Europe/Stockholm');
$now = new DateTime(date('Y-m'));
$period = new DatePeriod($now, new DateInterval('P1M'), 3);

foreach ($period as $date)
{
    print $date->format('MY');
}

Upvotes: 2

Rawkode
Rawkode

Reputation: 22592

echo date('F Y') . "\n";
echo date('F Y', strtotime('+1 month', time())) . "\n";
echo date('F Y', strtotime('+2 month', time())) . "\n";
echo date('F Y', strtotime('+3 month', time())) . "\n";

Upvotes: 2

jimpic
jimpic

Reputation: 5520

$t = time();
$m = date('n', $t);
$d = date('j', $t);
$y = date('Y', $t);
for ($i = 0; $i < 4; $i++)
{
    echo date('FY\n', mktime(0, 0, 0, ($m + $i - 1) % 12 + 1, $d, $y + ($m + $i > 12 ? 1 : 0)));
}

Upvotes: 0

Related Questions