PHP resolution for Strtotime ("-X Month") Bug
Strtotime ('-X month '); You may not get the expected results when it comes to month changes.
This is a bug in PHP:
https://bugs.php.net/bug.php?id=27793
?
For example: Current time: 2011-08-31 17:21:22
Date_default_timezone_set (' Asia/shanghai ');
$t = time ();
Print_r (Array (
??? ??? ??? Date (' Y year m month ', $t),
??? ??? ??? Date (' Y year m month ', Strtotime ('-1 month ', $t)),
??? ??? ??? Date (' Y year m month ', Strtotime ('-2 month ', $t)),
));
?>
The above code output:
Array
(
??? [0] = August 2011
??? [1] = July 2011
??? [2] = July 2011
)
And the expected result is:
Array
(
??? [0] = August 2011
??? [1] = July 2011
??? [2] = June 2011
)
?
============================================
?
Can be resolved as follows:
Date_default_timezone_set (' Asia/shanghai ');
$first _day_of_month = Date (' Y-m ', Time ()). '-01 00:00:01 ';
$t = Strtotime ($first _day_of_month);
Print_r (Array (
??? ??? ??? Date (' Y year m month ', $t),
??? ??? ??? Date (' Y year m month ', Strtotime ('-1 month ', $t)),
??? ??? ??? Date (' Y year m month ', Strtotime ('-2 month ', $t)),
));
?>
Output:
Array
(
??? [0] = August 2011
??? [1] = July 2011
??? [2] = June 2011
)