PHP resolution for Strtotime ("-X Month") Bug

Source: Internet
Author: User
Tags echo date greenwich time zone
Before the development project, encountered the use of Strtotime ("-1 months") will be a bug, mainly because the default PHP time is set in Greenwich time zone standard, there are two ways to solve this problem, one is to modify the default time zone in PHP.ini, and the other is to set the

Let's start with the first method, modify the default time zone in PHP.ini

I used the WAMP integration environment, previously thought to be in the bin/php/php5.5.12 to modify the default time zone, but no matter how the changes are still displayed in the Greenwich time zone, and the Internet did not find the corresponding solution, and later in bin/apache/ Apache2.4.9/bin found a php.ini file, set the file's time zone to Date.timezone = PRC, then the page is the same as my system time.

The method of setting is to modify the time zone for our region in the php.ini file, such as modifying Date.timezone = PRC to set the time to the People's Republic of China time, that is, the current time of our system, note: In order for us to set the time zone to take effect, we need to remove the previous;

The second method is to add Date_default_timezone_set (' PRC ') to the program;

Such as

<?php
Date_default_timezone_set (' PRC ');//This sentence is used to eliminate the time difference
$date = Date ("y-m-d h:i:s");

Echo $date; Output

?>

This will allow us to output our current time.

Now there is another problem, in the time of 2016-03-31, when the above method to obtain the time of the last one months, the acquisition time is still 2016-03, the initial assumption is because this method is to find the number of days last month, and then use today's time minus the number of days of the previous month.

Here are a few ways to help us achieve the desired results, such as the month I want to return to last month:
echo Date (' Y-m ', Strtotime (' Midnight First day of-1 month ');
Or:
echo Date (' Y-m ', strtotime (date (' y-m-01 ')) –86400);

Or do the following in the program

<?php
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)),
));
?>

This is done by separating the year and month of each month, then adding the days and minutes later, so that when you do Strtotime ('-1 month ', $t), you can show the last month's time.

In addition, mention a note:

When processing time with Strtotime (date ("y-m-d h:i:s"), the letters here are case-sensitive,

The h is case-sensitive, the lowercase h output is the hour of the time zone, and 17 points is 5 points. And the capital H is the output 17,

And the case of the year Y is different, the lowercase y output is 13. The uppercase Y output is 2013.

  • 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.