PHP to get the last month, next month, this month's date _ PHP Tutorial

Source: Internet
Author: User
PHP obtains the date of the previous month, next month, and this month. Because the work needs to get the date of the previous month, next month, and this month, we found the implementation code from the website and shared it to help our friends write programs today, I suddenly found that I needed to get the date of last month, next month, and this month for my work. I found the implementation code from my website.

When I was writing a program today, I suddenly found a function that was written a long time ago to get the number of days of the month. The classic switch version, but when I got the number of days of the previous month, I only set the month to 1, it is estimated that it was too difficult at the time, and then I saw a sense of treasure. I wanted to deal with it again, but I was sure there were some very convenient methods. so I found the following version, A small modification was made. Get Month date: Code: function getMonth ($ date) {$ firstday = date ("Y-m-01", strtotime ($ date )); $ lastday = date ("Y-m-d", strtotime ("$ firstday + 1 month-1 day"); return array ($ firstday, $ lastday );} $ firstday is the first day of the month. if $ date is like 2014-2, $ firstday will be. Then, add a month based on $ firstday to, and then subtract one day from, it is really convenient to use date () and strtotime. Get Last Month date: Code: function getlastMonthDays ($ date) {$ timestamp = strtotime ($ date); $ firstday = date ('Y-m-01 ', strtotime (date ('Y', $ timestamp ). '-'. (date ('M', $ timestamp)-1 ). '-01'); $ lastday = date ('Y-m-D', strtotime ("$ firstday + 1 month-1 day ")); return array ($ firstday, $ lastday);} you need to get a timestamp for the last month's date, and then-1 in the month is OK. the intelligent date () it would be nice to convert 2014-0-1. Get the next month date: the code is as follows: function getNextMonthDays ($ date) {$ timestamp = strtotime ($ date); $ arr = getdate ($ timestamp ); if ($ arr ['mon'] = 12) {$ year = $ arr ['Year'] + 1; $ month = $ arr ['mon']-11; $ firstday = $ year. '-0 '. $ month. '-01'; $ lastday = date ('Y-m-D', strtotime ("$ firstday + 1 month-1 day "));} else {$ firstday = date ('Y-m-01 ', strtotime (date ('Y', $ timestamp ). '-'. (date ('M', $ timestamp) + 1 ). '-01'); $ lastday = date ('Y-m-D', strtotim E ("$ firstday + 1 month-1 day");} return array ($ firstday, $ lastday);} the code for the next month's date looks longer, because date () if it cannot be changed to something like 2014-13-01, it will directly return to 1970. Therefore, we need to handle the issue of December before, except that the month of December is the month + 1. In general, it is very convenient. the date function is too powerful.

Today, when I wrote a program, I suddenly found out that...

Related Article

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.