PHP:strtotime()提供错误的输出

In my script, I have a given end date. To get the start date, I subtract 23 months to the end date. Basically, what my script should do is to output 24 months (w/ year) - the last month/year to be printed should always be the specified end date.

For some reason, my script isn't returning my desired results. Given the $end = '2013-07-05', the script returns the result correctly. It prints out Aug 11 to Jul 13 which is correct.

But for some dates (e.g. $end = '2013-07-31'), the output is wrong. The result should be Sep 11 to Aug 13. But in this case, it outputs Aug 11 to Aug 13 which is absolutely wrong.

Here's my code:

<?php
$end = strtotime('2013-07-31 +1 month');
$date = strtotime('2013-07-31 -23 month');
$start = $month = $date;
$months = "";
while($month < $end)
{
     $months .= date('M y', intval($month))." ";
     $month = strtotime("+1 month", intval($month));
}
echo $months;
?>

I think there's something wrong with strtotime(). Thanks in advance.

Based on Marc B's answer I modified the script to deal with the 29,30,31 of each month. What I did was, if the date is 29, 30, or 31, it will be subtracted with 3 days so that the date will be either 28 or below and would work just fine with the current code that I have. It worked for me so I guess I'll just stick with this for now. Here's the updated code:

<?php
$dt = "2013-07-31";
$dy = strtotime($dt);
$day = date("d", $dy);

if (($day == 29) || ($day == 30) || ($day == 31)){
     $dt = strtotime("$dt -3 days");
     $dt = date('Y-m-d', $dt);
}

$end = strtotime("$dt +1 month");
$date = strtotime("$dt -23 month");
$start = $month = $date;
$months = "";
while($month < $end)
{
     $months .= date('M y', intval($month))." ";
     $month = strtotime("+1 month", intval($month));
}
echo $months;
?>

Thanks for your help and insights. :)

You can't really use month calculations like that, especially when dealing with end-of-month values:

e.g. if it's July 31, what's -1 month to strtotime?

php > echo date('r', strtotime('2013-07-31 -1 month'));
Mon, 01 Jul 2013 00:00:00 -0600

A human would probably pick out June 30th, but strtotime isn't human. This DOES work for February 28th and generally any date where the day value is <= 28. Once you get into the 29,30,31 area, then you get these unexepected results

php > echo date('r', strtotime('2013-04-28 -1 month'));
Thu, 28 Mar 2013 00:00:00 -0600

How about

$endMonth = '8';
$year = '2013';

$i = 24;
while( $i > 0 ){
    $month = ($endMonth - $i)%12;
    if( $month == 0 ){
        $year = $year - 1;
        $month = 12;
    }
    $months .= date('M y', strtotime($year.'-'.$month.'-02'));
    $i--;
}