santa
santa

Reputation: 12512

How to show the past 24 months from now?

I need to write a simple loop routine to show 24 months back starting with today's month. How would I do that?

$start = date(M) - 24;
$end = date(M);

foreach() {
    echo ''; // Dec, Jan...
}

Upvotes: 1

Views: 364

Answers (2)

Kermit
Kermit

Reputation: 34053

Something like this should work:

for($i = 1; $i <= 24; $i++) {
    echo date("M", strtotime("-$i months")) . "\n";
}

Result

Feb
Jan
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar

See a demo

Upvotes: 3

user1193035
user1193035

Reputation:

for ($i = 1; $i <= 24; $i++) {
    $months[] = date("Y-m%", strtotime( date( 'Y-m-01' )." -$i months"));
}

or for full textual representation of month you need to pass "F":

echo date("y:F:d");

for previous month you can use

echo date("y:F:d",strtotime("-24 Months"))

Upvotes: 1

Related Questions