PHP resolution for Strtotime ("x month") Bug

Source: Internet
Author: User
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
)

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.