How to use PHP to calculate the previous month's _ PHP Tutorial

Source: Internet
Author: User
How to use PHP to calculate today of the previous month. One day, I encountered a problem. please wait for the day of the previous month. At the beginning, we used the strtotime ("-1month") function to evaluate the value. we found that there was a problem: the calculation of the month with different month lengths ends with one day, and we encountered a problem, requesting the day of the previous month. At the beginning, we used the strtotime ("-1 month") function to evaluate the value. we found that there was a problem where the calculation results of months with different monthly lengths were incorrect. For example, the result is 2011-03-03. First, let's look at how to solve the problem first. Now, I think that PHP has a mktime function, so I wrote the following code:

The code is as follows:


Echo date ("Y-m-d H: I: s", mktime (date ("G", $ time), date ("I", $ time ),
Date ("s", $ time), date ("n", $ time)-1, date ("j", $ time), date ("Y ", $ time )));


When executed, it is found that the results are the same as those of strtotime.
Based on this function, since the month cannot be operated directly, we can start with the day, get the previous month, and then use date to splice data. The following code:

The code is as follows:


$ Time = strtotime ("2011-03-31 ");
/**
* Calculate the day of the previous month
* @ Param type $ time
* @ Return type
*/
Function last_month_today ($ time ){
$ Last_month_time = mktime (date ("G", $ time), date ("I", $ time ),
Date ("s", $ time), date ("n", $ time),-1, date ("Y", $ time ));
Return date ("Y-m", $ last_month_time). "-d H: I: s", $ time );
}
Echo last_month_today ($ time );


But now there is another problem. there is no such date as. what should I do? The current requirement is to display the last day of the current month for such a date. The following code:

The code is as follows:


$ Time = strtotime ("2011-03-31 ");
/**
* Calculate the current day of the previous month. if the previous month does not have today, return the last day of the previous month.
* @ Param type $ time
* @ Return type
*/
Function last_month_today ($ time ){
$ Last_month_time = mktime (date ("G", $ time), date ("I", $ time ),
Date ("s", $ time), date ("n", $ time), 0, date ("Y", $ time ));
$ Last_month_t = date ("t", $ last_month_time );
If ($ last_month_t <date ("j", $ time )){
Return date ("Y-m-t H: I: s", $ last_month_time );
}
Return date ("Y-m", $ last_month_time). "-d", $ time );
}
Echo last_month_today ($ time );


Note the following code: date ("Y-m", $ last_month_time). "-d. During code writing, if "Y-". date ("m", $ last_month_time). "-d" is written, there is a problem with the time of the new year. This was found at the time of writing this article.
In addition to this method, you can first calculate the year, month, and day, and then splice the string. here is the pure string operation.

Bytes. At the beginning, we used the strtotime ("-1 month") function to evaluate the value. we found that there was a problem with the calculation of months with different monthly lengths...

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.