Candra
Candra

Reputation: 9

php mktime need a list of 24 months before this month

I need a list of 24 months before this month. Here is my code:

<?php
$monthbefore1  = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$monthbefore2  = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$monthbefore3  = mktime(0, 0, 0, date("m")-2, date("d"), date("Y"));
$monthbefore4  = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
$monthbefore5  = mktime(0, 0, 0, date("m")-4, date("d"), date("Y"));
$monthbefore6  = mktime(0, 0, 0, date("m")-5, date("d"), date("Y"));
$monthbefore7  = mktime(0, 0, 0, date("m")-6, date("d"), date("Y"));
$monthbefore8  = mktime(0, 0, 0, date("m")-7, date("d"), date("Y"));
$monthbefore9  = mktime(0, 0, 0, date("m")-8, date("d"), date("Y"));
$monthbefore10 = mktime(0, 0, 0, date("m")-9, date("d"), date("Y"));
$monthbefore11 = mktime(0, 0, 0, date("m")-10, date("d"), date("Y"));
$monthbefore12 = mktime(0, 0, 0, date("m")-11, date("d"), date("Y"));
$monthbefore13 = mktime(0, 0, 0, date("m")-12, date("d"), date("Y"));
$monthbefore14 = mktime(0, 0, 0, date("m")-13, date("d"), date("Y"));
$monthbefore15 = mktime(0, 0, 0, date("m")-14, date("d"), date("Y"));
$monthbefore16 = mktime(0, 0, 0, date("m")-15, date("d"), date("Y"));
$monthbefore17 = mktime(0, 0, 0, date("m")-16, date("d"), date("Y"));
$monthbefore18 = mktime(0, 0, 0, date("m")-17, date("d"), date("Y"));
$monthbefore19 = mktime(0, 0, 0, date("m")-18, date("d"), date("Y"));
$monthbefore20 = mktime(0, 0, 0, date("m")-19, date("d"), date("Y"));
$monthbefore21 = mktime(0, 0, 0, date("m")-20, date("d"), date("Y"));
$monthbefore22 = mktime(0, 0, 0, date("m")-21, date("d"), date("Y"));
$monthbefore23 = mktime(0, 0, 0, date("m")-22, date("d"), date("Y"));
$monthbefore24 = mktime(0, 0, 0, date("m")-23, date("d"), date("Y"));

echo date("F", $monthbefore24);
echo date("F", $monthbefore23);
echo date("F", $monthbefore22);
echo date("F", $monthbefore21);
echo date("F", $monthbefore20);
echo date("F", $monthbefore19);
echo date("F", $monthbefore18);
echo date("F", $monthbefore17);
echo date("F", $monthbefore16);
echo date("F", $monthbefore14);
echo date("F", $monthbefore13);
echo date("F", $monthbefore12);
echo date("F", $monthbefore11);
echo date("F", $monthbefore10);
echo date("F", $monthbefore9);
echo date("F", $monthbefore8);
echo date("F", $monthbefore7);
echo date("F", $monthbefore6);
echo date("F", $monthbefore5);
echo date("F", $monthbefore4);
echo date("F", $monthbefore3);
echo date("F", $monthbefore2);
echo date("F", $monthbefore1);
?>

Why does the result look like this? *MarchMarch*AprilMayJuneJulyAugustSeptemberOctoberDecemberJanuary*MarchMarch*AprilMayJuneJulyAugustSeptemberOctoberNovemberDecemberJanuary

Upvotes: 0

Views: 797

Answers (5)

James
James

Reputation: 2669

Your most likely getting a repeating March because your using today's DAY which is the 30th and not all months have 30 days. You can use these this

    $months = array();
    $date = mktime(0, 0, 0, date('n'), 1);
    for($i = -1; $i >= -24; $i--) {
        $months[] = date('F', strtotime($i.' month', $date));
    }
    print_r($months);

I've put the result in an array for ease of use but you could simple just echo out the results:

    $date = mktime(0, 0, 0, date('n'), 1);
    for($i = -1; $i >= -24; $i--) {
        echo date('F', strtotime($i.' month', $date));
    }

The first line creates a timestamp consisting of today's month and year but with the first day of the month. The third line then removes $i number of months from the date and converts it the resulting timestamp into a string representation of the month.

Upvotes: 1

SimonSimCity
SimonSimCity

Reputation: 6572

I would solve your problem in a different way. My question is: What do you expect by the month 0 of this year? Or the month -1 of this year? Look at the documentation of date() to find it out.

I'd use what PHP provides for date-calculation. The DateTime bundle.

Here's an example:

// Get an instance of DateTime with current time (no argument)
$date = new DateTime();

// Create a DateInterval (that may be a bit confusing) by one month
$interval = new DateInterval('P1M');
# Same as: $interval = DateInterval::createFromDateString('1 month')

// What date do we have now?
echo $date->format('F');

// What date is it if we substract one month and print it?
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');
echo $date->sub($interval)->format('F');

Upvotes: 0

Laurence
Laurence

Reputation: 60058

for ($i=-1; $i--; $i>-24)
{
      echo date("F", strtotime($i." months"));
}

Upvotes: 0

SoniCoder
SoniCoder

Reputation: 4174

today is January 30th, in February there is no day with 30, so it adds the plus 2 days to the date, and it results in March take a look at the PHP date function manual http://www.php.net/manual/en/function.date.php

Upvotes: 2

Miky
Miky

Reputation: 49

Try looking for date() manual and describe what you want to get.

Upvotes: 0

Related Questions